[Image-SIG] PIL and converting an image to and from a string value
Chris Hare
Chare at labr.net
Fri Apr 27 17:59:09 CEST 2012
Thanks! That did it.
The second part of my function is to drop the image in the StringIO buffer into a Tkinter canvas widget.
If I do this:
self.imageBuffer.seek(0)
image = Image.open(self.imageBuffer)
self.picture1.create_image(0, 0, image = image )
self.picture1 = Canvas(self.pictureFrame,width=150,height=150)
I get:
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "z.py", line 802, in changePicture1
self.picture1.create_image(0, 0, image = image )
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2198, in create_image
return self._create('image', args, kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2189, in _create
*(args + self._options(cnf, kw))))
TclError: image "<PngImagePlugin.PngImageFile image mode=RGB size=150x150 at 0x10F1227A0>" doesn't exist
If I change the code to use the image rest from Image.open above to create a TkImage.PhotoImage,
self.imageBuffer.seek(0)
image = Image.open(self.imageBuffer)
photo = PhotoImage(image)
self.picture1.create_image(0, 0, image = photo )
I get yet a different exception:
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "z.py", line 803, in changePicture1
self.picture1.create_image(0, 0, image = photo )
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2198, in create_image
return self._create('image', args, kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2189, in _create
*(args + self._options(cnf, kw))))
TypeError: __str__ returned non-string (type instance)
Not sure what I got wrong. Based on what I think I understand fro the man pages and some net searches, I think either case should be okay.
On Apr 27, 2012, at 9:55 AM, Gareth Rees wrote:
> Chris Hare wrote:
>> okay - I have an error but I don't understand what I got wrong:
>>
>> self.imageBuffer = StringIO.StringIO()
>> image = Image.open(filename)
>> image = image.resize((150,150),Image.ANTIALIAS)
>> image.save(self.imageBuffer, format= 'PNG')
>> image = Image.open(self.imageBuffer)
>> IOError: cannot identify image file
>>
>> The error occurs when trying to open the imageBuffer so I can work with the data in the buffer. What have I missed?
>
> You need to rewind to the start of the buffer before trying to read from it:
>
> self.imageBuffer.seek(0)
>
> --
> Gareth Rees
More information about the Image-SIG
mailing list