#!/usr/bin/env python3"""Write the generated cards and stamps into `apps/loom/Sources/CottonPaper/Media.xcassets`. python3 scripts/gen-cards/export_cards.py <out-dir from make_cards.py>Every card is cut with the MASTER's alpha box — not its own — so the sixpapers share one geometry and a card changes colour under its letteringwithout moving a pixel. The box is the master's alpha above 24 (the softshadow is part of the card here: it is what makes the paper lie on theplaster), and each cut is resized to 720 px wide, landscape 1.59:1, writtenat 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; aname the code draws and this list lacks is the failure `ProjectCapsTests`exists for.Needs Pillow (`python3 -m pip install pillow`)."""import json, pathlib, sysfrom PIL importImageROOT = 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 = 720STAMP_WIDTH = 480ALPHA = 24def alpha_box(im): box = im.getchannel("A").point(lambda v: 255if v > ALPHA else0).getbbox()if box isNone:raiseSystemExit("nothing opaque in the picture")return boxdef 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 / MASTERifnot master.exists():raiseSystemExit(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 / fileifnot f.exists(): print("skip", name)continue write(name, cut(f, box, CARD_WIDTH))for name, file in STAMPS.items(): f = src / fileifnot 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()