-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
199 lines (154 loc) · 5 KB
/
script.py
File metadata and controls
199 lines (154 loc) · 5 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
from __future__ import annotations
import argparse
from itertools import permutations
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
swatches = {
"a": [(248, 109, 142), (125, 132, 80), (71, 71, 73)],
"b": [(223, 180, 85), (8, 72, 159), (211, 6, 47)],
"c": [(200, 113, 42), (51, 95, 132), (109, 116, 126)],
"d": [(0, 0, 0), (255, 255, 255)],
"e": [(106, 85, 80), (242, 210, 172)],
}
def rgb_to_hex(r: int, g: int, b: int) -> str:
return "#{:02X}{:02X}{:02X}".format(r, g, b).lower()
DEFAULT_TEXT = "61418"
PALETTES = [
(f"{swatch_name}{ii}", rgb_to_hex(*bg), rgb_to_hex(*fg))
for swatch_name, swatch in swatches.items()
for ii, (bg, fg) in enumerate(permutations(swatch, 2))
]
def unique_colors(
swatches_dict: dict[str, list[tuple[int, int, int]]],
) -> list[tuple[int, int, int]]:
seen = set()
out: list[tuple[int, int, int]] = []
for _, cols in swatches_dict.items():
for c in cols:
if c not in seen:
seen.add(c)
out.append(c)
return out
TRANSPARENT_COLORS = unique_colors(swatches)
OUT_DIR = Path("assets")
SPECS = [
("favicon", (512, 512)),
("og", (1200, 630)),
("header", (1600, 300)),
]
def load_font(px: int) -> ImageFont.FreeTypeFont:
root = Path(__file__).resolve().parent
font_path = root / "Helvetica.ttc"
for idx in (1, 0):
try:
return ImageFont.truetype(str(font_path), px, index=idx)
except OSError:
continue
raise OSError(f"Could not load font from {font_path}")
def best_font_size(
draw: ImageDraw.ImageDraw,
text: str,
size: tuple[int, int],
width_fill: float,
height_fill: float,
) -> int:
w, h = size
target_w = int(w * width_fill)
max_h = int(h * height_fill)
lo, hi = 5, 2000
best = lo
while lo <= hi:
mid = (lo + hi) // 2
font = load_font(mid)
bbox = draw.textbbox((0, 0), text, font=font)
tw = bbox[2] - bbox[0]
th = bbox[3] - bbox[1]
if tw <= target_w and th <= max_h:
best = mid
lo = mid + 1
else:
hi = mid - 1
return best
def render_logo_solid(
text: str,
size: tuple[int, int],
bg_hex: str,
fg_hex: str,
width_fill: float,
height_fill: float,
) -> Image.Image:
w, h = size
img = Image.new("RGB", (w, h), bg_hex)
draw = ImageDraw.Draw(img)
font_px = best_font_size(draw, text, size, width_fill, height_fill)
font = load_font(font_px)
bbox = draw.textbbox((0, 0), text, font=font)
tw = bbox[2] - bbox[0]
th = bbox[3] - bbox[1]
x = (w - tw) // 2 - bbox[0]
y = (h - th) // 2 - bbox[1]
draw.text((x, y), text, font=font, fill=fg_hex)
return img
def render_logo_transparent(
text: str,
size: tuple[int, int],
fg_rgb: tuple[int, int, int],
width_fill: float,
height_fill: float,
) -> Image.Image:
w, h = size
img = Image.new("RGBA", (w, h), (0, 0, 0, 0)) # fully transparent background
draw = ImageDraw.Draw(img)
font_px = best_font_size(draw, text, size, width_fill, height_fill)
font = load_font(font_px)
bbox = draw.textbbox((0, 0), text, font=font)
tw = bbox[2] - bbox[0]
th = bbox[3] - bbox[1]
x = (w - tw) // 2 - bbox[0]
y = (h - th) // 2 - bbox[1]
r, g, b = fg_rgb
draw.text((x, y), text, font=font, fill=(r, g, b, 255))
return img
def fills_for(spec_name: str) -> tuple[float, float]:
if spec_name == "favicon":
return (0.78, 0.78)
return (0.72, 0.72)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--text",
default=DEFAULT_TEXT,
help=f"Text to render in logo images (default: {DEFAULT_TEXT})",
)
return parser.parse_args()
def output_dirs_for(text: str) -> tuple[Path, Path]:
if text == DEFAULT_TEXT:
base_dir = OUT_DIR
else:
base_dir = OUT_DIR / "project" / text
solid_dir = base_dir / "solid"
transparent_dir = base_dir / "transparent"
solid_dir.mkdir(parents=True, exist_ok=True)
transparent_dir.mkdir(parents=True, exist_ok=True)
return solid_dir, transparent_dir
def main():
args = parse_args()
text = args.text
solid_dir, transparent_dir = output_dirs_for(text)
for palette_name, bg, fg in PALETTES:
for spec_name, size in SPECS:
wf, hf = fills_for(spec_name)
img = render_logo_solid(text, size, bg, fg, wf, hf)
out = solid_dir / f"{palette_name}_{spec_name}.png"
img.save(out, optimize=True)
print("wrote", out)
for rgb in TRANSPARENT_COLORS:
hex_name = rgb_to_hex(*rgb)[1:] # drop '#'
for spec_name, size in SPECS:
wf, hf = fills_for(spec_name)
img = render_logo_transparent(text, size, rgb, wf, hf)
out = transparent_dir / f"{hex_name}_{spec_name}.png"
img.save(out, optimize=True)
print("wrote", out)
if __name__ == "__main__":
main()