#!/usr/bin/env python3 """Put the README screenshots into a browser window on a dawn gradient. Raw captures live in docs/screenshots/raw/.png at SCALE device pixels per CSS pixel (scripts/capture-screenshots.mjs takes them); the framed images are written to docs/screenshots/.png at the same scale. Needs a Chromium-based browser for the headless render (BROWSER=/path/to/chrome, default: Brave or Chrome on macOS). Run: scripts/frame-screenshots.py """ import os import shutil import struct import subprocess import sys import tempfile ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) RAW = os.path.join(ROOT, 'docs', 'screenshots', 'raw') OUT = os.path.join(ROOT, 'docs', 'screenshots') # name -> what the address bar shows SHOTS = { 'overview': 'router.lan · Dawnline', 'people': 'router.lan · Dawnline · People', 'person': 'router.lan · Dawnline · Myata', 'portal': 'vpn.example.com/kamiro-telosu-bavine', 'access': 'router.lan · Dawnline · Home · Access', } PAD_X, PAD_Y, BAR = 120, 96, 40 SCALE = 2 CANDIDATES = [ os.environ.get('BROWSER', ''), '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser', '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', shutil.which('chromium') or '', shutil.which('google-chrome') or '', ] TEMPLATE = """
{url}
""" def png_size(path): with open(path, 'rb') as f: head = f.read(24) return struct.unpack('>II', head[16:24]) def main(): browser = next((b for b in CANDIDATES if b and os.path.exists(b)), None) if not browser: sys.exit('No Chromium-based browser found; set BROWSER=/path/to/chrome') tmp = tempfile.mkdtemp() for name, url in SHOTS.items(): src = os.path.join(RAW, f'{name}.png') if not os.path.exists(src): print(f'skip {name}: no raw capture') continue iw, ih = (v // SCALE for v in png_size(src)) w, h = iw + 2 * PAD_X, ih + BAR + 2 * PAD_Y html = os.path.join(tmp, f'{name}.html') with open(html, 'w', encoding='utf-8') as f: f.write(TEMPLATE.format(w=w, h=h, iw=iw, ih=ih, bar=BAR, url=url, src=src, gw=int(w * .8), gh=int(h * .5), gw2=int(w * .6), gh2=int(h * .45))) out = os.path.join(OUT, f'{name}.png') subprocess.run([browser, '--headless=new', '--disable-gpu', '--hide-scrollbars', '--allow-file-access-from-files', f'--force-device-scale-factor={SCALE}', f'--window-size={w},{h}', f'--screenshot={out}', f'file://{html}'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) print(f'{name}: {w}x{h}') shutil.rmtree(tmp) if __name__ == '__main__': main()