| 1 | """Build official theme backgrounds + thumbnails and report budgets/hashes.""" |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | import argparse |
| 5 | import json |
| 6 | import os |
| 7 | import sys |
| 8 | |
| 9 | from artkit import H, W, make_thumb, save_webp, sha256_file |
| 10 | from scenes import SCENES |
| 11 | |
| 12 | OUT = os.path.join(os.path.dirname(__file__), "out") |
| 13 | BG_BUDGET = int(2.25 * 1024 * 1024) |
| 14 | THUMB_BUDGET = 120 * 1024 |
| 15 | TOTAL_BUDGET = 18 * 1024 * 1024 |
| 16 | |
| 17 | |
| 18 | def main(): |
| 19 | ap = argparse.ArgumentParser() |
| 20 | ap.add_argument("themes", nargs="*", default=None, help="theme ids to build (default: all)") |
| 21 | ap.add_argument("--out", default=OUT) |
| 22 | args = ap.parse_args() |
| 23 | |
| 24 | ids = args.themes or sorted(SCENES) |
| 25 | report = {} |
| 26 | total = 0 |
| 27 | for tid in ids: |
| 28 | if tid not in SCENES: |
| 29 | print(f"unknown theme {tid}", file=sys.stderr) |
| 30 | sys.exit(2) |
| 31 | img = SCENES[tid]() |
| 32 | assert img.size == (W, H), img.size |
| 33 | tdir = os.path.join(args.out, tid) |
| 34 | bg_path = os.path.join(tdir, "background.webp") |
| 35 | th_path = os.path.join(tdir, "preview.webp") |
| 36 | bg_size = save_webp(img, bg_path, quality=82, target_bytes=BG_BUDGET) |
| 37 | th_size = make_thumb(img, th_path, target_bytes=THUMB_BUDGET) |
| 38 | total += bg_size |
| 39 | report[tid] = { |
| 40 | "background_bytes": bg_size, |
| 41 | "background_ok": bg_size <= BG_BUDGET, |
| 42 | "background_sha256": sha256_file(bg_path), |
| 43 | "preview_bytes": th_size, |
| 44 | "preview_ok": th_size <= THUMB_BUDGET, |
| 45 | "preview_sha256": sha256_file(th_path), |
| 46 | } |
| 47 | print(f"{tid}: bg={bg_size/1024:.0f} KiB ok={report[tid]['background_ok']} thumb={th_size/1024:.0f} KiB") |
| 48 | print(f"total backgrounds: {total/1024/1024:.2f} MiB (budget 18 MiB)") |
| 49 | with open(os.path.join(args.out, "report.json"), "w") as f: |
| 50 | json.dump(report, f, indent=2) |
| 51 | |
| 52 | |
| 53 | if __name__ == "__main__": |
| 54 | main() |
| 55 |