-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_icon.py
More file actions
205 lines (152 loc) · 6.04 KB
/
Copy pathmake_icon.py
File metadata and controls
205 lines (152 loc) · 6.04 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
Icon Generator - Reusable for any Python project.
Requires: pip install pillow
"""
from PIL import Image, ImageDraw, ImageFont
import os
# ═══════════════════════════════════════════
# CONFIG
# ═══════════════════════════════════════════
STYLE = "nodes" # Options: "monogram", "nodes"
LETTER = "N"
BG_COLOR = "#ff5c7c"
FG_COLOR = "#ffffff"
USE_GRADIENT = True
GRADIENT_START = "#ff5c7c"
GRADIENT_END = "#c94a63"
CORNER_RADIUS_RATIO = 0.22
OUTPUT_NAME = "icon"
OUTPUT_DIR = "app/assets"
SIZES = [16, 32, 48, 64, 128, 256]
# ═══════════════════════════════════════════
# HELPERS
# ═══════════════════════════════════════════
def hex_to_rgb(hex_color):
hex_color = hex_color.lstrip('#')
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
# ═══════════════════════════════════════════
# DRAWING
# ═══════════════════════════════════════════
def draw_rounded_background(size):
"""Create rounded square background with gradient."""
img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
radius = int(size * CORNER_RADIUS_RATIO)
if USE_GRADIENT:
start_rgb = hex_to_rgb(GRADIENT_START)
end_rgb = hex_to_rgb(GRADIENT_END)
grad = Image.new('RGB', (size, size), start_rgb)
grad_draw = ImageDraw.Draw(grad)
for y in range(size):
ratio = y / size
r = int(start_rgb[0] + (end_rgb[0] - start_rgb[0]) * ratio)
g = int(start_rgb[1] + (end_rgb[1] - start_rgb[1]) * ratio)
b = int(start_rgb[2] + (end_rgb[2] - start_rgb[2]) * ratio)
grad_draw.line([(0, y), (size, y)], fill=(r, g, b))
mask = Image.new('L', (size, size), 0)
mask_draw = ImageDraw.Draw(mask)
mask_draw.rounded_rectangle([0, 0, size, size], radius=radius, fill=255)
img.paste(grad, (0, 0), mask)
else:
draw = ImageDraw.Draw(img)
draw.rounded_rectangle([0, 0, size, size], radius=radius, fill=BG_COLOR)
return img
def draw_monogram(img, size, letter):
"""Big letter centered."""
draw = ImageDraw.Draw(img)
font_size = int(size * 0.6)
font = None
for font_name in ["seguisb.ttf", "arialbd.ttf", "arial.ttf"]:
try:
font = ImageFont.truetype(font_name, font_size)
break
except (OSError, IOError):
continue
if font is None:
font = ImageFont.load_default()
bbox = draw.textbbox((0, 0), letter, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
x = (size - text_width) // 2 - bbox[0]
y = (size - text_height) // 2 - bbox[1]
shadow_offset = max(1, size // 128)
draw.text((x + shadow_offset, y + shadow_offset), letter,
font=font, fill=(0, 0, 0, 60))
draw.text((x, y), letter, font=font, fill=FG_COLOR)
return img
def draw_nodes(img, size):
"""Connected nodes - neural network look."""
draw = ImageDraw.Draw(img, 'RGBA')
nodes_relative = [
(0.30, 0.30),
(0.70, 0.30),
(0.50, 0.55),
(0.30, 0.75),
(0.70, 0.75),
]
positions = []
for rx, ry in nodes_relative:
positions.append((int(rx * size), int(ry * size)))
line_width = max(2, size // 60)
line_color = (255, 255, 255, 200)
for i in range(len(positions)):
for j in range(i + 1, len(positions)):
draw.line([positions[i], positions[j]],
fill=line_color, width=line_width)
node_radius = max(4, size // 15)
for x, y in positions:
draw.ellipse(
[x - node_radius, y - node_radius,
x + node_radius, y + node_radius],
fill=(255, 255, 255, 255)
)
return img
# ═══════════════════════════════════════════
# MAIN
# ═══════════════════════════════════════════
def create_icon(size):
img = draw_rounded_background(size)
if STYLE == "monogram":
img = draw_monogram(img, size, LETTER)
elif STYLE == "nodes":
img = draw_nodes(img, size)
return img
def main():
print("=" * 60)
print(f" Icon Generator")
print(f" Style: {STYLE}")
print(f" Colors: {BG_COLOR} + {FG_COLOR}")
print("=" * 60)
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Generate all sizes as PIL Image objects
images = []
for size in SIZES:
img = create_icon(size)
images.append(img)
print(f" Generated {size}x{size}")
# Save PNG (largest size for preview/README)
png_path = os.path.join(OUTPUT_DIR, f"{OUTPUT_NAME}.png")
images[-1].save(png_path, format='PNG')
print(f"\n Saved: {png_path} ({os.path.getsize(png_path)} bytes)")
# Save ICO with ALL sizes properly embedded
ico_path = os.path.join(OUTPUT_DIR, f"{OUTPUT_NAME}.ico")
# CRITICAL FIX: Pass sizes as tuple of (width, height) tuples
# and use the largest image as base
images[-1].save(
ico_path,
format='ICO',
sizes=[(s, s) for s in SIZES],
)
ico_size = os.path.getsize(ico_path)
print(f" Saved: {ico_path} ({ico_size} bytes)")
if ico_size < 5000:
print(f"\n WARNING: ICO file is only {ico_size} bytes")
print(f" Expected 15,000+ bytes. Icon may be corrupted!")
else:
print(f"\n ICO file looks healthy!")
# Preview
preview_path = f"{OUTPUT_NAME}_preview.png"
images[-1].save(preview_path, format='PNG')
print(f" Preview: {preview_path}")
print("=" * 60)
if __name__ == "__main__":
main()