Image object etching?

I'm a complete pickle newbie and have a bunch (about 100,000) of images to pickle.

They are first loaded as an image object and converted to data like this:

image = {
    'pixels': im.tostring(),
    'size': im.size,
    'mode': im.mode,
}

      

Now, how do I collect them into one pkl file?

+3


source to share


2 answers


You can do it like this:

file = open('data.pkl', 'wb')

# Pickle dictionary using protocol 0.
pickle.dump(image, file)
file.close()

      

For reading in a dictionary, you can do it like this:



file = open('data.pkl', 'rb')

image = pickle.load(pkl_file)
print image
file.close()

      

You can also dump data twice:

import pickle

# Write to file.
file = open("data.pkl", "wb")
pickle.dump(image1, file)
pickle.dump(image2, file)
file.close()

# Read from file.
file = open("data.pkl", "rb")
image1 = pickle.load(file)
image2 = pickle.load(file)
file.close()

      

+4


source


Just call pickle.dump

, just like you would for everything else. You have a dict whose values ​​are simple types (strings, tuples of a pair of numbers, etc.). The fact that he came from an image doesn't matter.

If you have a bunch of them, they appear to be stored in a list or some other structure, and you can make a list of favorites.



So:

with open('data.pkl', 'wb') as f:
    pickle.dump(images, f)

      

+2


source







All Articles