Files

78 lines
3.7 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import argparse
import html
import json
import re
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
HERE = Path(__file__).resolve().parent
WIDTH, HEIGHT = 1024, 500
BROWSERS = ("chromium-browser", "chromium", "google-chrome", "google-chrome-stable")
PAGE = """<!doctype html><meta charset="utf-8"><style>
@font-face{{font-family:Name;src:url('{font}')}}
@font-face{{font-family:Line;src:url('{mono}')}}
*{{margin:0;padding:0;box-sizing:border-box}}
html,body{{width:{width}px;height:{height}px;overflow:hidden}}
body{{background:{paper};color:{ink};--ink:{ink};--accent:{accent};--well:{well};position:relative}}
.rule{{position:absolute;left:0;right:0;height:1px;background:{rule}}}
.wrap{{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;gap:56px}}
.seal{{flex:none;width:236px;height:236px;border-radius:50%;background:{seal};display:grid;place-items:center;box-shadow:inset 0 0 0 1px {rule}}}
.seal svg{{width:{mark_size}px;height:{mark_size}px;display:block}}
.name{{font-family:Name;font-size:{size}px;letter-spacing:{tracking};line-height:1.05;white-space:nowrap}}
.line{{margin-top:22px;display:flex;align-items:center;gap:18px;font-family:Line;font-size:20px;letter-spacing:.14em;text-transform:uppercase;color:{accent}}}
.key{{width:72px;height:4px;background:{key}}}
</style>
<div class="rule" style="top:64px"></div><div class="rule" style="bottom:64px"></div>
<div class="wrap"><div class="seal">{mark}</div><div><div class="name">{name}</div><div class="line"><span class="key"></span>{line}</div></div></div>
"""
def browser():
for name in BROWSERS:
found = shutil.which(name)
if found:
return found
sys.exit(f"none of {', '.join(BROWSERS)} is installed")
def page(spec):
mark = (HERE / spec["mark"]).read_text(encoding="utf-8")
mark = re.sub(r"^.*?(?=<svg)", "", mark, flags=re.S)
return PAGE.format(
width=WIDTH, height=HEIGHT, font=(HERE / spec["font"]).as_uri(), mono=(HERE / "fonts/FiraCode-Regular-ascii.woff2").as_uri(),
mark=mark, mark_size=spec["mark-size"], size=spec["size"], tracking=spec["tracking"],
name=html.escape(spec["name"]), line=html.escape(spec["line"]), **spec["colours"])
def render(spec, target):
from PIL import Image
with tempfile.TemporaryDirectory() as work:
source, shot = Path(work) / "banner.html", Path(work) / "banner.png"
source.write_text(page(spec), encoding="utf-8")
subprocess.run([browser(), "--headless", "--disable-gpu", "--no-sandbox", "--hide-scrollbars", "--force-device-scale-factor=1",
f"--window-size={WIDTH},{HEIGHT}", "--virtual-time-budget=5000", f"--screenshot={shot}", source.as_uri()],
check=True, capture_output=True)
with Image.open(shot) as image:
if image.size != (WIDTH, HEIGHT):
sys.exit(f"the browser drew {image.size[0]}x{image.size[1]}, not {WIDTH}x{HEIGHT}")
target.parent.mkdir(parents=True, exist_ok=True)
image.convert("RGB").save(target, "PNG", optimize=True)
def main():
specs = json.loads((HERE / "banners.json").read_text(encoding="utf-8"))
parser = argparse.ArgumentParser(description="Draw an app's store banner (Google Play's feature graphic, F-Droid's header), 1024x500, no alpha.")
parser.add_argument("app", choices=sorted(specs), help="the app, as named in banners.json")
parser.add_argument("target", type=Path, help="where to write it, normally fastlane/metadata/android/en-US/images/featureGraphic.png")
args = parser.parse_args()
render(specs[args.app], args.target)
print(args.target)
if __name__ == "__main__":
main()