Dynamic image creation for the web...

Benjamin Niemann pink at odahoda.de
Sun Aug 28 17:54:33 EDT 2005


Tompa wrote:

> Benjamin Niemann <pink <at> odahoda.de> writes:
>> You are almost there.
> I don't feel so...
> 
>> Your create_image.py does not return anything to the
>> browser yet.
> Yes, I am aware of that but I do not what to return.
> 
>> First return proper HTTP headers, e.g.
>> 
>> sys.stdout.write('Status: 200 OK\r\n')
>> sys.stdout.write('Content-type: image/gif\r\n')
>> sys.stdout.write('\r\n')
> 
> Ok, but if possible I'd rather not return anything HTTP/HTML-related from
> my create_image.py file.

When the browser fetches the images for displaying, it performs just another
HTTP request, and you must reply with a valid HTTP response. The
Content-type header is the absolute minimum that must always be returned.
(IIRC the 'Status' can be omitted, if it's 200).

>> Then check the PIL docs to find out, how to output the image to
>> sys.stdout (instead of writing to a file).
>> 
> Ok, then I get this:
> 
> from PIL import Image, ImageDraw
> import sys
> 
> im = Image.new("P", (600, 400))
> draw = ImageDraw.Draw(im)
> draw.rectangle((0, 0) + im.size, fill="blue")
> 
> sys.stdout.write('Status: 200 OK\r\n')
> sys.stdout.write('Content-type: image/gif\r\n')
> sys.stdout.write('\r\n')
> 
> im.save(sys.stdout, "GIF")
> 
> But this does not work.
> I also tested to skip the HTTP-header stuff and just write the gif to
> sys.stdout, believing that that would work. But not so...

Works perfectly here...
What does the error.log of the webserver say?

> Hmm, I'm a newbie to Python (as you already probably have noticed ;-) so I
> don't know what else I should try. Any hints are welcome!
> 
> /Tompa

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/



More information about the Python-list mailing list