forked from Teskann/QuaX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_icons.py
More file actions
96 lines (84 loc) · 2.76 KB
/
generate_icons.py
File metadata and controls
96 lines (84 loc) · 2.76 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import cairosvg
import os
from pathlib import Path
from PIL import Image
BACKGROUND_COLOR = "#080808"
def generate_icon_png(svg_file):
output_file = svg_file.replace('.svg', '.png')
cairosvg.svg2png(
url=svg_file,
write_to=output_file,
output_width=2000,
output_height=2000,
background_color=BACKGROUND_COLOR
)
print("Generated", output_file)
def generate_icon_png_for_readme(svg_file):
readme_dir = Path(__file__).parent / "assets" / "readme"
output_file = readme_dir / "icon.png"
cairosvg.svg2png(
url=svg_file,
write_to=str(output_file),
output_width=2000,
output_height=2000,
background_color=BACKGROUND_COLOR
)
img = Image.open(output_file).convert("RGBA")
width, height = img.size
radius = int(min(width, height) * 0.25)
mask = Image.new("L", (width, height), 0)
from PIL import ImageDraw
draw = ImageDraw.Draw(mask)
draw.rounded_rectangle(
[(0, 0), (width, height)],
radius=radius,
fill=255
)
result = Image.new("RGBA", (width, height), (0, 0, 0, 0))
result.paste(img, (0, 0))
result.putalpha(mask)
result.save(str(output_file), "PNG")
print("Generated", output_file)
def generate_adaptive_foreground(svg_file):
base_name = os.path.splitext(svg_file)[0]
output_file = f"{base_name}-foreground-432x432.png"
cairosvg.svg2png(
url=svg_file,
write_to=output_file,
output_width=432,
output_height=432,
)
print("Generated", output_file)
def generate_adaptive_monochrome(svg_file):
base_name = os.path.splitext(svg_file)[0]
output_file = f"{base_name}-monochrome-432x432.png"
cairosvg.svg2png(
url=svg_file,
write_to=output_file,
output_width=432,
output_height=432,
)
img = Image.open(output_file).convert("RGBA")
mono_img = Image.new("RGBA", (432, 432), (0, 0, 0, 0))
for x in range(432):
for y in range(432):
r, g, b, a = img.getpixel((x, y))
if a > 0:
mono_img.putpixel((x, y), (255, 255, 255, a))
mono_img.save(output_file, "PNG")
print("Generated", output_file)
def generate_adaptive_background(svg_file):
base_name = os.path.splitext(svg_file)[0]
output_file = f"{base_name}-background.png"
img = Image.new("RGB", (432, 432), BACKGROUND_COLOR)
img.save(output_file, "PNG")
print("Generated", output_file)
def main():
svg_file = str(Path(__file__).parent / "assets" / "icon.svg")
generate_icon_png(svg_file)
generate_icon_png_for_readme(svg_file)
generate_adaptive_foreground(svg_file)
generate_adaptive_monochrome(svg_file)
generate_adaptive_background(svg_file)
if __name__ == "__main__":
main()