PIL: reading bytes from Image

Max Erickson maxerickson at gmail.com
Sat Mar 10 19:44:52 EST 2007


"cyberco" <cyberco at gmail.com> wrote:

> I'm using web.py to send an image to the client. This works
> (shortened):
> 
> print open(path, "rb").read()
> 
> but this doesn't:
> 
> img = Image.open(path)
> img.thumbnail((10,10))
> print img.getdata()
> 
> or
> 
> print img.load()
> 
> 
> How do I get the bytes of the Image object? 'getdata()' seemed the
> way, but unfortunately...
> 

getdata() returns the data in a PIL format. Saving the image, in a 
format your client understands, to a file like object like 
StringIO.StringIO is an easy path to take.

Sparklines shows this in action:

http://bitworking.org/projects/sparklines/


max




More information about the Python-list mailing list