forked from istvan-antal/font-awesome-to-png
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_icon.py
55 lines (37 loc) · 1.56 KB
/
export_icon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from PIL import Image, ImageFont, ImageDraw
def export_icon(icons, icon, size, filename, fontname, color):
# Initialize font
fontsize = size
font = ImageFont.truetype(fontname, fontsize)
image = Image.new("RGBA", (size, size), color=(0,0,0,0))
draw = ImageDraw.Draw(image)
# Determine the dimensions of the icon
width,height = draw.textsize(icons[icon], font=font)
fontsize = int(float(size) / float(max(width, height)) * size)
font = ImageFont.truetype(fontname, fontsize)
image = Image.new("RGBA", (size, size), color=(0,0,0,0))
draw = ImageDraw.Draw(image)
# Determine the dimensions of the icon
width,height = draw.textsize(icons[icon], font=font)
draw.text(((size - width) / 2, (size - height) / 2), icons[icon],
font=font, fill=color)
# Get bounding box
bbox = image.getbbox()
# Create an alpha mask
imagemask = Image.new("L", (size, size), 0)
drawmask = ImageDraw.Draw(imagemask)
# Draw the icon on the mask
drawmask.text(((size - width) / 2, (size - height) / 2), icons[icon],
font=font, fill=255)
# Create a solid color image and apply the mask
iconimage = Image.new("RGBA", (size,size), color)
iconimage.putalpha(imagemask)
if bbox:
iconimage = iconimage.crop(bbox)
borderw = int((size - (bbox[2] - bbox[0])) / 2)
borderh = int((size - (bbox[3] - bbox[1])) / 2)
# Create output image
outimage = Image.new("RGBA", (size, size), (0,0,0,0))
outimage.paste(iconimage, (borderw,borderh))
# Save file
outimage.save(filename)