views:

29

answers:

1

I need to take a list of words and create a PNG file of each, with a transparent background.

I’d like my script to allow for adjustable opacity of the foreground, but I can probably also do it after the fact when the images get imported into Matlab.

I imagine this is doable with ImageMagick and have installed that on my Mac. If someone can give me the one line I’d need to turn a word into a PNG (the word can be the filename too) in Perl or Python, I can probably figure out the rest of the scripting.

A: 

Matplotlib (better known as pylab) would work great for this. Assuming ImageMagick is installed this script should work. (tested on Ubuntu 9.10)

import pylab as py, os

W = ['cat','dog','mouse']

for word in W:
    py.clf()
    ax = py.axes(alpha=1.0) 
    py.text(0,.5,word)
    py.axis('off')
    py.savefig("%s.png"%word)
    os.system('convert -trim %s.png %s.png' % (word,word))

This creates three files on my system dog.png, cat.png and mouse.png all cropped with a transparent background.

Hooked
Sweet, I’ll give it a try this weekend!
yonatron
If this answer is what you were looking for, please mark it or let us know what else you need!
Hooked