Pickle an image?

G

GMane Python

Hey all.
I have a ( list | tuple | dictionary ) with several graphics in it.
Trying to cPickle it to a file, I get errors. For instance,
UnpickleableError: Cannon pickle <type 'ImagingCore'> objects.

Anyone know of a way to get around this? I'd like to pickle a list or
dictionary of about 5 .JPG images.

Thanks!
Dave
 
M

Michael Hoffman

GMane said:
I have a ( list | tuple | dictionary ) with several graphics in it.
Trying to cPickle it to a file, I get errors. For instance,
UnpickleableError: Cannon pickle <type 'ImagingCore'> objects.

Anyone know of a way to get around this? I'd like to pickle a list or
dictionary of about 5 .JPG images.

Have you tried converting the image objects to strings and back?
 
M

M.E.Farmer

GMane said:
Hey all.
I have a ( list | tuple | dictionary ) with several graphics in it.
Trying to cPickle it to a file, I get errors. For instance,
UnpickleableError: Cannon pickle <type 'ImagingCore'> objects.

Anyone know of a way to get around this? I'd like to pickle a list or
dictionary of about 5 .JPG images.

Thanks!
Dave
Open your images in binary mode and read them in as binary strings then
save them in a list/dict/tuple , then pickle it.# storage would happen at this point

Ok now you take your pickled string and unpickle your object and select
the item you want and put it in a cStringIO or StringIO so we can open
it directly with PIL.
# sometime in the future
o = pickle.loads(p)
one_im = o['1']
c = StringIO.StringIO()
c.write(one_im)
c.seek(0)
im = Image.open(c)
im.show()

Alternatively you can create a StringIO object and pickle that in your
dictionary so you can just load it later with PIL.

Pickling will bloat the size of your file so you might want to use some
compression, although most formats will compress very little the
pickling overhead can __mostly__ be avoided.
There are boundless ways to do this and this just one of them.
hth,
M.E.Farmer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,236
Messages
2,571,185
Members
47,820
Latest member
HortenseKo

Latest Threads

Top