Skip to content

Commit 2661103

Browse files
committed
First version of logo-shrinking script
1 parent 97e13ef commit 2661103

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

shrink_logos.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from pathlib import Path
2+
from PIL import Image
3+
4+
# The logos are constrained in CSS to 160 px by 80 px. We allow up to double
5+
# that size, as high-resolution displays may display more than one pixel in a
6+
# CSS pixel.
7+
MAX_WIDTH = 160 * 2
8+
MAX_HEIGHT = 80 * 2
9+
10+
ASSETS_DIR = Path(__file__).parent / 'assets'
11+
12+
for filename in ASSETS_DIR.glob('*.png'):
13+
im = Image.open(filename)
14+
w, h = im.size
15+
if w <= MAX_WIDTH and h <= MAX_HEIGHT:
16+
# This image is already small enough
17+
continue
18+
19+
# It should fit in both dimensions, so take the smaller scale from them.
20+
w_scale = MAX_WIDTH / w
21+
h_scale = MAX_HEIGHT / h
22+
scale = min(w_scale, h_scale)
23+
24+
new_w = int(w * scale)
25+
new_h = int(h * scale)
26+
27+
print(f"Resizing {filename} from {w}×{h} to {new_w}×{new_h}.")
28+
29+
im.resize((new_w, new_h)).save(filename)

0 commit comments

Comments
 (0)