PIL: reading bytes from Image

Max Erickson maxerickson at gmail.com
Sun Mar 11 01:44:52 CET 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:



More information about the Python-list mailing list