#!/usr/bin/env python import sys import cgi import cgitb import urllib import Image, ImageDraw, ImageFont cgitb.enable() form = cgi.FieldStorage() img = form["i"].value if not img.startswith("http:"): raise ValueError("%s not an HTTP url" % img) im = Image.open(urllib.urlretrieve(img)[0]) if "c" in form: cap = form["c"].value font = ImageFont.truetype("fonts/LiberationSans-Regular.ttf", 12) (w, h) = font.getsize(cap) draw = ImageDraw.Draw(im) (left, top, right, bottom) = im.getbbox() box = (0, bottom - 30, right, bottom) mask = Image.new("L", (right, 30), 111) im.paste("black", box, mask) center = (right - left ) / 2 x = center - (w/2) if x < 0: x = 3 draw.text((x, bottom - 20), cap, font=font) del draw fmt = "png" print "Content-type: image/%s" % fmt print print im.save(sys.stdout, format=fmt)