Created
September 3, 2014 05:33
-
-
Save sshopov/ee7a5cdeee925024d9e3 to your computer and use it in GitHub Desktop.
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. Tags: Python, PIL, Pillow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires http://www.pythonware.com/products/pil/ or https://github.com/python-pillow/Pillow | |
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment