Re: [Image-SIG] PIL and converting an image to and from a string value
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 "" 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 ___ Image-SIG maillist - Image-SIG@python.org http://mail.python.org/mailman/listinfo/image-sig
Re: [Image-SIG] PIL and converting an image to and from a string value
On Fri, Apr 27, 2012 at 6:21 AM, Charles Cazabon wrote: >> I am stuck however, in figuring out how to take the string data and >> converting it back to an image that I can put into the canvas widget. > > See the PIL handbook, where it says "If you have an entire image file in a > string, wrap it in a StringIO object, and use open to load it." Actually, I don't think that's what the OP wants. A couple notes: 1) "string" is a confusing term -- generally it means text, but in python (2.*) it means "an arrbitrary sequence of bytes, that can be interpreteed as text depending on context" -- that is why there is now a "string" object and a "bytes" object. IN py2, bytes and string are the same, but it's handy to use bytes for future compatibility with py3, and for clarity in your code. So in this case, we are talking about bytes objects. (side note: make sure you are storing bytes objects in your DB properly -- as a binary blob, so that it doens't mess with the data) Now the real point: you can store an image in two ways (at least) in a bytes object: 1) essentially a binary dump of what would be in an encoced file (PNG, whatever). This is would you d get if you did: the_image_bytes = file("an_image.png", 'rb').read() 2) a binary dump of the bytes in memeory of the image object, without encoding or compression -- this is waht you get whn you do: the_image_bytes = a_pil_image.tostring() The STringIO suggestion refers to the former, but I thikn the OP was dealing with the later. In the later case, you can re-construct teh image object with: Image.fromstring(mode, size, data) => image so: a_pil_image = Image.fromstring('RGB', (300, 500), the_image_bytes) note that you'll need to keep track of what the made and size of the image are -- PIL can't figure that out just from the data. Depending on your needs, you may in fact want to store the PNG-encoded bytes in the DB, and decode them when you pull them out, using the StringIO strick. HTH, -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov ___ Image-SIG maillist - Image-SIG@python.org http://mail.python.org/mailman/listinfo/image-sig
Re: [Image-SIG] PIL and converting an image to and from a string value
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 ___ Image-SIG maillist - Image-SIG@python.org http://mail.python.org/mailman/listinfo/image-sig
Re: [Image-SIG] PIL and converting an image to and from a string value
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? On Apr 27, 2012, at 8:21 AM, Charles Cazabon wrote: > Chris Hare wrote: >> >> I am stuck however, in figuring out how to take the string data and >> converting it back to an image that I can put into the canvas widget. > > See the PIL handbook, where it says "If you have an entire image file in a > string, wrap it in a StringIO object, and use open to load it." > > Charles > -- > -- > Charles Cazabon > Software, consulting, and services available at http://pyropus.ca/ > -- > ___ > Image-SIG maillist - Image-SIG@python.org > http://mail.python.org/mailman/listinfo/image-sig ___ Image-SIG maillist - Image-SIG@python.org http://mail.python.org/mailman/listinfo/image-sig
Re: [Image-SIG] PIL and converting an image to and from a string value
Chris Hare wrote: > > I am stuck however, in figuring out how to take the string data and > converting it back to an image that I can put into the canvas widget. See the PIL handbook, where it says "If you have an entire image file in a string, wrap it in a StringIO object, and use open to load it." Charles -- -- Charles Cazabon Software, consulting, and services available at http://pyropus.ca/ -- ___ Image-SIG maillist - Image-SIG@python.org http://mail.python.org/mailman/listinfo/image-sig