Dynamic image creation for the web...

Tompa tompa1969 at yahoo.com
Sun Aug 28 05:50:17 EDT 2005


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 '<HTML><HEAD><TITLE>Python Dynamic Image Creation Test</TITLE></HEAD>'
print '<BODY>'
im = Image.new("P", (600, 400))
draw = ImageDraw.Draw(im)
draw.rectangle((0, 0) + im.size, fill="blue")
im.save("images/tmp.gif");
print '<img src="/scripts/images/tmp.gif">'
print '</BODY>'


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):
<html>
<head><meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
 <title>Python Dynamic Image Creation</title>
</head>
<IMG SRC="/scripts/create_image.py" ALT="Image created on the fly...">
</html>

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





More information about the Python-list mailing list