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 http://www.pythonware.com/products/pil/
  2. import Image, ImageFont, ImageDraw
  3.  
  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 = Image.open(r'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')
  13. #image.show()
  14. image.save(r'C:\python\temp\bd%s.png'%number)
  15.  
  16. for i in range(1,101):
  17. add_number(str(i))

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.