From 26611037d7455125ba6e28c7cfbc2172c16c0c5f Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Fri, 26 Apr 2019 08:55:11 +0100 Subject: [PATCH] First version of logo-shrinking script --- shrink_logos.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 shrink_logos.py diff --git a/shrink_logos.py b/shrink_logos.py new file mode 100644 index 0000000..3a6067c --- /dev/null +++ b/shrink_logos.py @@ -0,0 +1,29 @@ +from pathlib import Path +from PIL import Image + +# The logos are constrained in CSS to 160 px by 80 px. We allow up to double +# that size, as high-resolution displays may display more than one pixel in a +# CSS pixel. +MAX_WIDTH = 160 * 2 +MAX_HEIGHT = 80 * 2 + +ASSETS_DIR = Path(__file__).parent / 'assets' + +for filename in ASSETS_DIR.glob('*.png'): + im = Image.open(filename) + w, h = im.size + if w <= MAX_WIDTH and h <= MAX_HEIGHT: + # This image is already small enough + continue + + # It should fit in both dimensions, so take the smaller scale from them. + w_scale = MAX_WIDTH / w + h_scale = MAX_HEIGHT / h + scale = min(w_scale, h_scale) + + new_w = int(w * scale) + new_h = int(h * scale) + + print(f"Resizing {filename} from {w}×{h} to {new_w}×{new_h}.") + + im.resize((new_w, new_h)).save(filename)