#!/usr/bin/env python3 """Regenerate the portable 256-color crest data.""" from __future__ import annotations import json import re import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parent.parent ASSETS = ROOT / "synadm_tui_thueringen" / "assets" PIXEL = re.compile(r"#([0-9A-Fa-f]{8})\s") def main() -> int: process = subprocess.run( ["convert", str(ASSETS / "thueringen-wappen.png"), "-filter", "Lanczos", "-resize", "18x20!", "txt:-"], check=True, text=True, stdout=subprocess.PIPE, ) pixels = [match.group(1).lower() for line in process.stdout.splitlines() if (match := PIXEL.search(line))] if len(pixels) != 18 * 20: raise RuntimeError(f"360 Pixel erwartet, {len(pixels)} erhalten") target = ASSETS / "thuringia.block.json" target.write_text( json.dumps({"width": 18, "height": 20, "pixels": pixels}, separators=(",", ":")) + "\n", encoding="utf-8", ) print(target) return 0 if __name__ == "__main__": raise SystemExit(main())