Generate numbered icons with PIL

/ Published in: Python
Save to your folder(s)

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.

Copy this code and paste it in your HTML
  1. #requires
  2. import Image, ImageFont, ImageDraw
  4. def add_number(number):
  5. '''Add the number to the center of an icon and save it to a new image
  6. Useful for generating multiple icons'''
  7. image ='C:\python\temp\bd.png')
  8. font=ImageFont.truetype("arial.ttf", 55) #, encoding='utf-8')
  9. draw=ImageDraw.Draw(image)
  10. x=(image.size[0]-draw.textsize(number,font)[0])/2
  11. y=(image.size[1]-draw.textsize(number,font)[1])/2
  12. draw.text((x,y),number,font=font,fill='#FFFFFF')
  16. for i in range(1,101):
  17. add_number(str(i))


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.