开发者

Automate conversion of text to PNG (or another file format with transparency)?

I need to take a list of words and create开发者_如何学Go 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.


I found this to be applicable to your question:

convert -size 560x85 xc:transparent -font Palatino-Bold -pointsize 72 -fill black -draw "text 20,55 'Linux and Life'" linuxandlife.png

Output:

Automate conversion of text to PNG (or another file format with transparency)?

  • size 560x85 : the size (width x height) of the image
  • xc:transparent : xc means X window color, use "transparent" to get a transparent background
  • fond Palatino-Bold : the font of the text ( type "identify -list font" in the terminal to know all the supported fonts)
  • pointsize 72 : the size of the text
  • fill black : the color of the text ( check this link to know the names of the supported colors)
  • draw "text 20,55 'Linux and Life'" : the position of the text and the text itself
  • linuxandlife.png : the output image


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜