"""Stitch built tiles into one overview PNG -- a layout sanity check. python tools/preview.py [zoom] # default 4 Writes build/world-preview.png showing the base map with every detail map composited on top at its world position, so you can confirm placements before opening a browser. Tile (z, x, y) sits at level pixel (x * tileSize, y * tileSize) because every map's grid shares the world origin -- padding is baked into the indices. """ from __future__ import annotations import json import sys from PIL import Image from common import BUILD, DATA, ROOT, TILES Image.MAX_IMAGE_PIXELS = None MAX_PX = 4000 def main() -> None: world_path = DATA / "world.json" if not world_path.exists(): raise SystemExit("web/data/world.json missing -- run tools/build.py first") world = json.loads(world_path.read_text(encoding="utf-8")) ts, zmax = world["tileSize"], world["maxZoom"] z = int(sys.argv[1]) if len(sys.argv) > 1 else 4 z = max(0, min(z, zmax)) scale = 2.0 ** (z - zmax) width = max(1, round(world["worldSize"][0] * scale)) height = max(1, round(world["worldSize"][1] * scale)) if max(width, height) > MAX_PX: raise SystemExit(f"zoom {z} would be {width}x{height}; try a lower zoom") canvas = Image.new("RGBA", (width, height), (16, 18, 22, 255)) for m in world["maps"]: # base first, already ordered by build.py # Mirror the browser: fall back to the deepest level a map actually has # and upscale it, so the base map still shows past its native zoom. src_z = next((zz for zz in range(z, -1, -1) if (TILES / m["id"] / str(zz)).exists()), None) if src_z is None: print(f" {m['id']:<18} no tiles at or below z{z}") continue up = 2 ** (z - src_z) tdir = TILES / m["id"] / str(src_z) n = 0 for col in tdir.iterdir(): for tile in col.glob("*.webp"): x, y = int(col.name), int(tile.stem) img = Image.open(tile).convert("RGBA") if up != 1: img = img.resize((ts * up, ts * up), Image.LANCZOS) canvas.alpha_composite(img, (x * ts * up, y * ts * up)) n += 1 note = "" if up == 1 else f" (z{src_z} upscaled x{up})" print(f" {m['id']:<18} origin {tuple(m['origin'])} size {tuple(m['size'])} {n} tiles{note}") out = BUILD / "world-preview.png" canvas.convert("RGB").save(out) print(f"\nwrote {out.relative_to(ROOT)} {width}x{height} (zoom {z} of {zmax})") if __name__ == "__main__": main()