Image to browser

Justin Ezequiel justin.mailinglists at gmail.com
Wed Jan 16 00:38:44 EST 2008


On Jan 16, 1:19 pm, danielatdavesch... at gmail.com wrote:
> On Jan 16, 12:16 am, danielatdavesch... at gmail.com wrote:
>
> > Im using mod_python and apache2 using psp for output of page, i open a
> > file and resize it with the following code
>
> > <%
> > import Image, util
>
> > fields = util.FieldStorage(req)
> > filename = fields.getlist('src')[0]
>
> > path = '/var/www/content/' + filename
> > size = 128, 128
>
> > im = Image.open(path)
> > print im.resize(size, Image.ANTIALIAS)
> > %>
>
> > so for one, I dont think print does much with psp as far as i can
> > tell, i cant even do a print 'hello world', it has to be a
> > req.write('hello world'), but i cant req.write the im.resize. The
> > manual for im.resize states that its return can go ahead and be
> > streamed via http but I get a blank page when im expecting to see
> > image characters dumped to my screen. Python doesn't throw up any
> > errors. Im not sure where else to look or what to do.
>
> > Thanks for any help,
> > Daniel
>
> its worth noting that ive tried using
> print "Content-Type: image/jpeg\n"
> before the print im.resize and still no luck

If you're using the Image module from PIL then im.resize(...) returns
an Image instance.
I have not used mod_python and psp, but try the following:

>>> import Image
>>> i = Image.open('server.JPG')
>>> r = i.resize((32,32))
>>> from StringIO import StringIO
>>> b = StringIO()
>>> r.save(b, 'JPEG')
>>> b.seek(0)
>>> req.write("Content-Type: image/jpeg\r\n\r\n")
>>> req.write(b.read())


There's a r.tostring(...) method but I don't see how to make that
return a JPEG stream.




More information about the Python-list mailing list