[Image-SIG] batch utility to generate buttons from text?
Rob W. W. Hooft
rob@hooft.net
Thu, 23 Aug 2001 17:48:51 +0200
>>>>> "RM" == Ruggier, Mario <Mario.Ruggier@softplumbers.com> writes:
RM> Hi folks, I wonder if anyone can point me to a utility that takes
RM> a string label as input, and combines it with a prepared 'blank'
RM> image template, to output an image file for the text
RM> button. Other parameters (such as font face, weight, size) may be
RM> set directly in the program.
Recently, I did exactly this. I downloaded a few pilfonts, and wrote
a special-purpose program to make all the banners. Even though this
is highly specialized to my situation, it may help you to figure it out.
This uses a PNG file "butbg.png" to read the background image used
for the label, and then draws the text in there. It uses the largest
of three fonts that will fit the text in the standard label width
of 400 pixels.
This code also contains a routine to create a color map for the
conversion to GIF (which is done using the pbmplus utilities). The map
used is the standard 216 color web map.
Please be careful with the Unisys license fees if you want to use
GIF.
Regards,
Rob Hooft
----------------------------------------------------
import os
from PIL import Image, ImageFont, ImageDraw
fonts=[ImageFont.load('/home/hooft/p/pilfonts/New_Fonts/PIL/Arial_100/Arial_32_100.pil'),
ImageFont.load('/home/hooft/p/pilfonts/New_Fonts/PIL/Arial_100/Arial_24_100.pil'),
ImageFont.load('/home/hooft/p/pilfonts/New_Fonts/PIL/Arial_100/Arial_20_100.pil')]
def makemap():
f=open('map.ppm','w')
f.write('P3\n216 1\n5\n')
for r in range(6):
for g in range(6):
for b in range(6):
f.write('%d %d %d\n'%(r,g,b))
f.close()
def make(text,filename,width=400):
im=Image.open("butbg.png")
if width<im.size[0]:
im=im.crop((im.size[0]-width,0,im.size[0],im.size[1]))
draw=ImageDraw.Draw(im)
iw,ih=im.size
for font in fonts:
w,h=draw.textsize(text,font=font)
if w<width-23:
break
if w>width-23:
raise "Too wide, %d of %d"%(w,width)
x=(iw-5)/2-w/2
y=(ih-5)/2-h/2-2
draw.text((x+3,y+3),text,fill=0,font=font)
draw.text((x,y),text,fill=(255*256+255)*256+255,font=font)
del draw
im.save('tmp.ppm','PPM')
os.system('ppmquant -fs -map map.ppm tmp.ppm | ppmtogif > %s'%filename)
os.unlink('tmp.ppm')
if __name__=="__main__":
makemap()
make('Hi there!','HiThere_txt.gif')
--
===== rob@hooft.net http://www.hooft.net/people/rob/ =====
===== R&D, Bruker Nonius BV, Delft http://www.nonius.nl/ =====
===== PGPid 0xFA19277D ================================= Use Linux! =========