Dynamic image creation for the web...
Hi, I would like to create images on the fly as a response to an http request. I can do this with PIL like this (file create_gif.py): from PIL import Image, ImageDraw print 'Status: 200 OK' print 'Content-type: text/html' print print 'Python Dynamic Image Creation Test' print '' im = Image.new("P", (600, 400)) draw = ImageDraw.Draw(im) draw.rectangle((0, 0) + im.size, fill="blue") im.save("images/tmp.gif"); print '' print '' However, I would like to 1) avoid saving the image in a file on disk and 2) separate the HTLM code from the python image creation code. Something like this is what I have in mind: (file index.html): Python Dynamic Image Creation and in file create_image.py: from PIL import Image, ImageDraw, ImageFont im = Image.new("P", (600, 400)) draw = ImageDraw.Draw(im) draw.rectangle((0, 0) + im.size, fill="blue") Unfortunately this does not work :-( What is missing? Thanks in advance! /Tompa -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic image creation for the web...
Benjamin Niemann 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. > 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... 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic image creation for the web...
Richard Lewis fastmail.co.uk> writes: > It would be useful to know what web server software you're using. I intended to add that info but forgot... I run IIS on W2K, python 2.4.1 and PIL 1.1.5. > The other thing you may need to check is the HTTP header of the > generated image. If possible I'd rather separate the HTTP/HTML-stuff from image creation. I'd like to have an HTML file that refers to a py-file that creates images which are returned somehow (sys.stdout or something else in memory) and incorporated within the HTTP-response. > It should be possible to create an HTTP response from > your create_image.py script (as opposed to just an image) with a MIME > type of image/jpeg and manually insert the binary image data in the > response body... Yes, I believe so too. Something like this, as suggested by Benjamin: 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 it does not work for some reason!? Besides, I was hoping for a solution which could skip the HTTP-header related stuff... Regards, /Tompa -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic image creation for the web...
Max Erickson gmail.com> writes: > > check out sparklines: > > http://bitworking.org/projects/sparklines/ > > It is a script very similar to what you want to do. This sure looks interesting! Strange that I couldn't find this when I googled for this kind of stuff... I will check it out - thanks! Regards, /Tompa -- http://mail.python.org/mailman/listinfo/python-list