473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pickle an image?

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

Jul 19 '05 #1
2 9061
GMane Python wrote:
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?
--
Michael Hoffman
Jul 19 '05 #2
GMane Python wrote:
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.
import Image, pickle, cStringIO
i = open('c:/1.jpg', 'rb')
i.seek(0)
w = i.read()
i.close()
dic = {'1':w,'2':w,'3':w,'4':w}
p = pickle.dumps(dic) # 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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from...
0
by: Mike P. | last post by:
Hi all, I'm working on a simulation (can be considered a game) in Python where I want to be able to dump the simulation state to a file and be able to load it up later. I have used the standard...
16
by: John Salerno | last post by:
Here's what I have: import pickle data = open(r'C:\pickle_data.txt', 'w') image = open(r'C:\peakhell.jpg') pickle.Pickler(data) data.dump(image) data.close() image.close()
6
by: Jim Lewis | last post by:
Pickling an instance of a class, gives "can't pickle instancemethod objects". What does this mean? How do I find the class method creating the problem?
10
by: crystalattice | last post by:
I'm creating an RPG for experience and practice. I've finished a character creation module and I'm trying to figure out how to get the file I/O to work. I've read through the python newsgroup...
5
by: Chris | last post by:
Why can pickle serialize references to functions, but not methods? Pickling a function serializes the function name, but pickling a staticmethod, classmethod, or instancemethod generates an...
0
by: eso40043 | last post by:
Hello, due to a memory leak (bug, I guess) in pyraf(or rather in IRAF) I have to fork an iterative process that goes through hundreds of image frames and does unspeakable things to them. In the...
3
by: fizilla | last post by:
Hello all! I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question: How to pickle a...
1
by: IceMan85 | last post by:
Hi to all, I have spent the whole morning trying, with no success to pickle an object that I have created. The error that I get is : Can't pickle 'SRE_Match' object: <_sre.SRE_Match object at...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.