pickling an image object?

26,456

Solution 1

You could do it like this:

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

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

To read in the dictionary, you can do it like this:

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

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

It is also possible to dump the 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()

Solution 2

Just call pickle.dump, the same way you would for anything else. You have a dict whose values are all simple types (strings, tuples of a couple numbers, etc.). The fact that it came from an image is irrelevant.

If you have a bunch of them, presumably they're stored in a list or some other structure, and you can pickle a list of pickleable objects.

So:

with open('data.pkl', 'wb') as f:
    pickle.dump(images, f)
Share:
26,456
ytrewq
Author by

ytrewq

Updated on July 18, 2022

Comments

  • ytrewq
    ytrewq almost 2 years

    I'm a complete novice with pickle, and I have a bunch of (about 100,000) images that need to be pickled.

    They are first loaded as image object, and converted to data as following:

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

    Now how do I pickle them into one pkl file?

  • ytrewq
    ytrewq almost 10 years
    if I have many image objects to pickle, then should I first put them in an array and then pickle it? or should I just keep dumping?
  • miindlek
    miindlek almost 10 years
    Yes, your should. But it is also possible to dump the data twice. Look at my update.
  • abarnert
    abarnert almost 10 years
    @CosmicRabbitMediaInc: Well, if you have many image objects to pickle, they should probably already be in a list. If you have the same code copied and pasted 40 times to use image_1, image_2, etc., don't do that.
  • martineau
    martineau over 9 years
    @CosmicRabbitMediaInc: You can write multiple object to the same pickle file, one by one, or put them all in a container like a list and write just that. The latter would probably be quicker. See my answer to the question yours was marked as duplicating.