"""Combine the QGIS renders from xyz_tile_catalogue.py into labelled contact sheets. Run with any Python that has Pillow, after xyz_tile_catalogue.py: python contact_sheet.py """ import json from pathlib import Path from PIL import Image, ImageDraw, ImageFont OUT = Path(__file__).parent / "output" RENDERS = OUT / "renders" def slug(name: str) -> str: s = "".join(c if c.isalnum() else "-" for c in name.lower()) while "--" in s: s = s.replace("--", "-") return s.strip("-") def font(size): for f in ("segoeui.ttf", "arial.ttf", "DejaVuSans.ttf"): try: return ImageFont.truetype(f, size) except OSError: pass return ImageFont.load_default() def sheet(items, cols, path, tile=(400, 250)): rows = (len(items) + cols - 1) // cols pad, label_h = 10, 30 w = cols * (tile[0] + pad) + pad h = rows * (tile[1] + label_h + pad) + pad img = Image.new("RGB", (w, h), "white") draw = ImageDraw.Draw(img) f = font(17) for i, (label, file) in enumerate(items): r, c = divmod(i, cols) x = pad + c * (tile[0] + pad) y = pad + r * (tile[1] + label_h + pad) im = Image.open(RENDERS / file).convert("RGB").resize(tile, Image.LANCZOS) img.paste(im, (x, y + label_h)) draw.rectangle([x, y + label_h, x + tile[0] - 1, y + label_h + tile[1] - 1], outline="#c8c8c8") draw.text((x, y + 4), label, fill="#1f2328", font=f) img.save(path, optimize=True) print(f"wrote {path.name}: {len(items)} panels, {w} x {h} px") def main(): checks = json.loads((OUT / "tile_checks.json").read_text(encoding="utf-8")) keswick = [(r["name"], f"{slug(r['name'])}.png") for r in checks if r["view"] == "keswick" and r["group"] != "Esri overlays"] keswick.append(("Esri imagery + roads + labels (hybrid)", "esri-hybrid.png")) national = [(r["name"], f"{slug(r['name'])}.png") for r in checks if r["group"] == "National"] sheet(keswick, 4, OUT / "contact-sheet-keswick.png") sheet(national, 4, OUT / "contact-sheet-national.png") if __name__ == "__main__": main()