Return to Snippet

Revision: 8480
at September 25, 2008 03:59 by stoyan


Initial Code
#requires http://www.pythonware.com/products/pil/
import Image, ImageFont, ImageDraw

def add_number(number):
    '''Add the number to the center of an icon and save it to a new image
    Useful for generating multiple icons'''
    image = Image.open(r'C:\python\temp\bd.png')
    font=ImageFont.truetype("arial.ttf", 55) #, encoding='utf-8')
    draw=ImageDraw.Draw(image)
    x=(image.size[0]-draw.textsize(number,font)[0])/2
    y=(image.size[1]-draw.textsize(number,font)[1])/2
    draw.text((x,y),number,font=font,fill='#FFFFFF')
    #image.show()
    image.save(r'C:\python\temp\bd%s.png'%number)

for i in range(1,101):
    add_number(str(i))

Initial URL
http://snipplr.com/?success

Initial Description
I had an icon representing a little blue dot and needed to create 100 copies each with a number in them. 
Python saves the day yet again.

Initial Title
Generate numbered icons with PIL

Initial Tags
python

Initial Language
Python