Quantcast

PIL and converting an image to and from a string value

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

PIL and converting an image to and from a string value

Chris Hare, CISSP, CISA, CISM

Here is what I am trying to:

the application user chooses an image file.  I want to store the image data in a field in a sqlite database.  My understanding from the reading I have done is that I have to convert the image data into a string , which I can then store in the database.  Additionally, I need to be able to take the stored image data and convert it back to an image  so I can display it in a canvas widget.  I am using the python imaging library

I think I have the following parts correct:

image = Image.open("cover.jpg")
image = image.resize((150,150),Image.ANTIALIAS)
png = image.tostring("PNG")

I am taking the image data from the file, resizing the image, and then using the PNG coder to force it into a PNG format image and converting it to a string.

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.  Either I am not finding a good explanation of how to do this or I am just not understanding what I am finding:-)

Any ideas are appreciated!

Thanks,
Chris

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PIL and converting an image to and from a string value

Charles Cazabon-21
Chris Hare <[hidden email]> 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                   <[hidden email]>
Software, consulting, and services available at http://pyropus.ca/
------------------------------------------------------------------
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PIL and converting an image to and from a string value

Chris Hare, CISSP, CISA, CISM

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 <[hidden email]> 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                   <[hidden email]>
> Software, consulting, and services available at http://pyropus.ca/
> ------------------------------------------------------------------
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PIL and converting an image to and from a string value

Gareth Rees
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  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PIL and converting an image to and from a string value

Chris Barker
In reply to this post by Charles Cazabon-21
On Fri, Apr 27, 2012 at 6:21 AM, Charles Cazabon
<[hidden email]> 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

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PIL and converting an image to and from a string value

Chris Hare, CISSP, CISA, CISM
In reply to this post by Gareth Rees

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

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Loading...