A while back I was interested in creating an “on the fly” sig generator using Python and ImageMagick. I started out by using ImageMagick calls from within Python, which is really a bass-ackwards way of doing things. I got turned on to the PythonMagick module but it really seems that development has stopped on it and there is no documentation.

In my travels, however, I got introduced to the Python Imaging Library, which is a really cool module library similar to what ImageMagick does. Not only does this allow me to use these image objects within Python, but it was already installed on both my home system as well as on 1&1 servers, who are my webhost. It’s definitely something worth checking out, but a brief tutorial here on how to make an image on the fly is below. One other cool thing that I want to mention is that you can load TrueType fonts in locally, which makes things very convenient for annotation.

So here we go, creating a new 550×125 pixel orange image with a 2×2 pixel internal black border.

# First, load the necessary modules
import Image, ImageDraw

# Now, create two images and then paste them together:
a = Image.new(“RGB”,(550,125),color=(0))
b = Image.new(“RGB”,(546,121),color=(255,125,0))
a.paste(b,(2,2))
a.show() # See where we are so far
Image 1

# Now load in a font at 18 point, here we use Abaddon
Abaddon=ImageFont.truetype(‘Abaddon.ttf’,18)
draw = ImageDraw.Draw(a)
draw.text((50,50),’This is a test’,font=Abaddon,fill=(0)) # fill makes this black
a.show() # See the updated image
Image 2
# Now save what we have created!
a.save(‘temp.png’,’png’)

And that’s pretty much all there is to it, but obviously there is tons more than you can do.

Technorati Tags:

Advertisements