''' A View that Returns a PNG Image generated using PIL'''
import Image, ImageDraw
size = (100,50) # size of the image to create
im = Image.new('RGB', size) # create the image
draw = ImageDraw.Draw(im) # create a drawing object that is
# used to draw on the new image
red = (255,0,0) # color of our text
text_pos = (10,10) # top-left position of our text
text = "Hello World!" # text to draw
# Now, we'll do the drawing:
draw.text(text_pos, text, fill=red)
del draw # I'm done drawing so I don't need this anymore
# We need an HttpResponse object with the correct mimetype
response = HttpResponse(mimetype="image/png")
# now, we tell the image to save as a PNG to the
# provided file-like object
return response # and we're done!
This example just draws simple text, but the drawing code could be replaced by something more elaborate such as code that opens and scales existing images.
A note of concern: The Image object (im) MUST be saved as a PNG for this to work. Luckily, the Image.save method expects a file-like object as its first parameter, so we can use django's HttpResponse object here. Also, the example above doesn't make use of any particular Font for the drawing, so if you do want to draw text, you'll want to take a look at PIL's ImageDraw Documentation.
(on a side note: the html for the code above was generated using dpaste, which rocks!)
comments powered by Disqus