#!/usr/bin/env python3 """Write the generated cards and stamps into `apps/loom/Sources/CottonPaper/Media.xcassets`. python3 scripts/gen-cards/export_cards.py Every card is cut with the MASTER's alpha box — not its own — so the six papers share one geometry and a card changes colour under its lettering without moving a pixel. The box is the master's alpha above 24 (the soft shadow is part of the card here: it is what makes the paper lie on the plaster), and each cut is resized to 720 px wide, landscape 1.59:1, written at the imageset's 2x scale (360pt intrinsic, two to a row on the phone). A stamp is trimmed on its own alpha (its ink has no shared geometry to keep) and written 480 px wide, 2x too. The set is `CARDS` and `STAMPS` below; a name the code draws and this list lacks is the failure `ProjectCapsTests` exists for. Needs Pillow (`python3 -m pip install pillow`). """ import json, pathlib, sys from PIL import Image ROOT = pathlib.Path(__file__).resolve().parents[2] CATALOGUE = ROOT / "apps/loom/Sources/CottonPaper/Media.xcassets" # catalogue name → generated file. Cream is the master. CARDS = { "card-cream": "card-a.png", "card-blue": "card-blue.png", "card-sage": "card-sage.png", "card-pink": "card-pink.png", "card-straw": "card-straw.png", "card-peach": "card-peach.png", } STAMPS = { "stamp-done": "stamp-a.png", "stamp-dropped": "stamp-dropped.png", } MASTER = CARDS["card-cream"] CARD_WIDTH = 720 STAMP_WIDTH = 480 ALPHA = 24 def alpha_box(im): box = im.getchannel("A").point(lambda v: 255 if v > ALPHA else 0).getbbox() if box is None: raise SystemExit("nothing opaque in the picture") return box def cut(path, box, width): im = Image.open(path).convert("RGBA").crop(box) return im.resize((width, round(im.height * width / im.width)), Image.LANCZOS) def write(name, image): dest = CATALOGUE / f"{name}.imageset" dest.mkdir(parents=True, exist_ok=True) image.save(dest / f"{name}.png", optimize=True) contents = { "images": [{"filename": f"{name}.png", "idiom": "universal", "scale": "2x"}], "info": {"author": "xcode", "version": 1}, } # Xcode's own spelling of the file (`"key" : value`), so the catalogue reads as one. (dest / "Contents.json").write_text(json.dumps(contents, indent=2, separators=(",", " : ")) + "\n") print("wrote", dest.relative_to(ROOT), image.size) def main(): src = pathlib.Path(sys.argv[1]) master = src / MASTER if not master.exists(): raise SystemExit(f"{master}: the master is missing — every card is cut with its box") box = alpha_box(Image.open(master).convert("RGBA")) for name, file in CARDS.items(): f = src / file if not f.exists(): print("skip", name) continue write(name, cut(f, box, CARD_WIDTH)) for name, file in STAMPS.items(): f = src / file if not f.exists(): print("skip", name) continue im = Image.open(f).convert("RGBA") write(name, cut(f, alpha_box(im), STAMP_WIDTH)) if __name__ == "__main__": main()