'Streaming' image from Python Imaging Library (PIL)

3,390 views
Skip to first unread message

Alex Davies

unread,
Nov 17, 2009, 4:54:58 PM11/17/09
to Tornado Web Server
Hi,

I need to produce some small images on the fly using PIL (http://
www.pythonware.com/products/pil/)

Is it possible to 'stream' these images through Tornado?

I guess I could write them to a file that is hosted statically and
redirect to that file, but this is a bit of a mess and i'd be happy to
take the performance hit for a neater solution.

Any help appreciated!

Many thanks,

Alex

Andrew Gwozdziewycz

unread,
Nov 17, 2009, 5:38:32 PM11/17/09
to python-...@googlegroups.com
All you really need to do is set the correct mime type and write the
file to the user instead of to disk. In otherwords:

import cStringIO
...
# do you image handling

imgbuff = cStringIO.StringIO()
image.save(imgbuff)
self.set_header('Content-Type', 'image/jpg')
self.write(imgbuff.read())

Should get you what you need, though this isn't tested. Actually, it
may be possible to to just do image.save(self), but I'm not sure...
Try it and see!

Andrew
--
http://www.apgwoz.com
Reply all
Reply to author
Forward
0 new messages