Kirbies
Kirbies

Reputation: 817

write images to csv files with python

I have a set of images and corresponding information I would like to store in a csv file like this:

file_info 1 | file_info2       | image
some info   |  other info      | 121 127 ...a lot of number ... 130 128 

This is just one example row, but there would be as many rows as I have images.

How would I do this? I have been trying with(again just for one row to begin with)

with open('Fail.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow(('some_info','other_info',image.flatten()))

This gives me three columns as it should, but the image cell is bad --instead of being 45163008 space delimited pixel values the cell just contains the abbreviated python output as [121 127 ..., 130 128] ... meaning, two problems...1) it does not write all the numbers out and 2) it prints the brackets.

I also tried pandas DataFrames, but there it was difficult to insert arrays/lists of numbers in individual cells.

Any suggestions?

Upvotes: 0

Views: 7900

Answers (1)

Malik Brahimi
Malik Brahimi

Reputation: 16711

Use base64 which encodes a binary data in the format of an ASCII string. Try the following code:

encodedImage = base64.b64encode(open('path/to/your/image.ext', 'rb').read())

Upvotes: 1

Related Questions