#!/usr/bin/env python3 """Downscale and re-encode the raw Playwright captures as WebP. Called by scripts/screenshots.mjs; not useful on its own. python3 scripts/screenshots-encode.py The captures are taken at 2x (desktop) and 3x (mobile) so text is sampled cleanly, then halved here. Halving rather than keeping the full capture is deliberate: a README renders images at roughly 850px wide, so a 2880px-wide PNG buys nothing but repository weight. WebP at q86 takes the forty-image set from about 16MB to under 2MB. """ from __future__ import annotations import pathlib import sys from PIL import Image QUALITY = 86 def main() -> int: if len(sys.argv) != 3: print(__doc__, file=sys.stderr) return 2 src = pathlib.Path(sys.argv[1]) dst = pathlib.Path(sys.argv[2]) dst.mkdir(parents=True, exist_ok=True) captures = sorted(src.glob("*.png")) if not captures: print(f"no PNG captures in {src}", file=sys.stderr) return 1 before = after = 0 for png in captures: image = Image.open(png).convert("RGB") image = image.resize((image.width // 2, image.height // 2), Image.LANCZOS) out = dst / f"{png.stem}.webp" image.save(out, "WEBP", quality=QUALITY, method=6) before += png.stat().st_size after += out.stat().st_size print(f"encoded {len(captures)} images: {before / 1e6:.1f}MB PNG -> {after / 1e6:.1f}MB WebP") return 0 if __name__ == "__main__": raise SystemExit(main())