[Tutor] PIL and converting an image to and from a string value

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

[Tutor] 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

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Chris Hare, CISSP, CISA, CISM

I got it figured out.  

On Apr 27, 2012, at 12:21 AM, Chris Hare wrote:

>
> 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
>
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Russell Smith-10
What did you do?

On Friday, April 27, 2012, Chris Hare wrote:

I got it figured out.

On Apr 27, 2012, at 12:21 AM, Chris Hare wrote:

>
> 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
>
> _______________________________________________
> Tutor maillist  -  <a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;Tutor@python.org&#39;)">Tutor@...
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  <a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;Tutor@python.org&#39;)">Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Chris Hare, CISSP, CISA, CISM
What I did:

self.imageBuffer = StringIO.StringIO()
image = Image.open(filename)
image = image.resize((150,150),Image.ANTIALIAS)
image.save(self.imageBuffer, format= 'PNG')
self.imageBuffer.seek(0)
image = Image.open(self.imageBuffer)

So, that is how I got around the problem


On Apr 27, 2012, at 1:22 PM, Russell Smith wrote:

What did you do?

On Friday, April 27, 2012, Chris Hare wrote:

I got it figured out.

On Apr 27, 2012, at 12:21 AM, Chris Hare wrote:

>
> 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
>
> _______________________________________________
> Tutor maillist  -  <a href="javascript:;" onclick="_e(event, 'cvml', 'Tutor@python.org')">Tutor@...
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  <a href="javascript:;" onclick="_e(event, 'cvml', 'Tutor@python.org')">Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Loading...