Cotton code
134 lines · 7.4 KB · PythonRaw
#!/usr/bin/env python3"""Generate the SIX blank card faces Home's deck is printed on.    python3 scripts/gen-cards/make_card_faces.py --out-dir <scratch>            # all six    python3 scripts/gen-cards/make_card_faces.py --out-dir <scratch> --only sage    python3 scripts/gen-cards/make_card_faces.py --out-dir <scratch> --master <a card.png>Home's tools are a DECK (`Paper/ToolCard.swift`): every tool is a portrait card,and the card is a PHOTOGRAPH of a blank piece of aged card stock with the ruleand the name printed on it LIVE — the enamel project caps' shape exactly(`scripts/gen-caps/`), and for the same reason: a card drawn in SwiftUI out of arounded rectangle, a grain tile and a stroked border is a drawing of a card, andit reads as one beside the photographed objects standing on it.**One master, five edits, and the sixth is an edit too.** The set has to share apaper, a border, a frame and a light, and six prompts give six cameras — so thefirst call makes ONE master card and every face, the cream one included, is anEDIT of that master with the panel re-papered. That is `make_caps.py`'s rule and`export_cards.py`'s rule; it is why the deck reads as a deck.The owner's reference sheet is the style anchor and travels as a secondreference image on every call: `--anchor` (a single card cut out of it — asixteen-card sheet as a reference makes the model draw a sheet).What a face carries, and what it must NOT:  • an ivory aged card-stock margin around the outside, the tinted panel inside    it, one hairline frame with 45-degree notched corners printed on the panel,    paper grain, foxing specks, a soft vignette, worn bumped edges;  • NOTHING else. No object, no rule, no name, no number. The rule and the name    are set live in SwiftUI and blended onto the photograph (`ToolCard`), so a    renamed tool is a redraw and not a regeneration — and the calendar's date    can be today's.The ground is flat magenta and `export_card_faces.py` keys it out, because thecard's own edge is worn and irregular: a rounded rectangle clipped in SwiftUIwould cut that edge off straight, which is the one detail that says photograph.Writes `<out-dir>/face-<tint>.png` (and `master.png`). Nothing here iscommitted — `export_card_faces.py` cuts the imagesets. Reads the OpenAI key theway every generator in this repo does: `OPENAI_API_KEY`, else`~/.config/cotton/openai.env`, and never prints it."""import argparseimport pathlibimport subprocessimport sysROOT = pathlib.Path(__file__).resolve().parents[2]GEN = ROOT / "scripts/gen-caps/gen_image.py"SIZE = "1024x1536"# The panel each face is papered in. The words are the PAPER's, never the# tool's: these are six stocks a printer had in the shop, and which tool ends up# on which is `ToolCardTint.byTool` — a colour that named a kind would be the# one thing the app's colour rule forbids.TINTS = {    "cream": "a warm aged CREAM, about #E6D2B6 — old ivory paper",    "sage": "a soft muted SAGE GREEN, about #B5B294 — a dusty grey-green, never bright",    "rose": "a soft DUSTY ROSE, about #D8B3A0 — a greyed, faded pink, never salmon or coral",    "slate": "a muted SLATE BLUE, about #A6B2BA — the pale grey-blue of faded denim, never a strong blue",    "tan": "a warm light TAN, about #D9BB96 — kraft paper, never orange or mustard",    "grey": "a quiet warm GREY, about #C4BBAE — a putty grey with no green in it",}MASTER_PROMPT = (    "A flat-on macro PHOTOGRAPH of ONE blank vintage collectible card from an old printed "    "set, exactly in the style and construction of the reference card, lying alone in the "    "centre of a flat pure magenta background with a wide magenta margin all around it. The "    "card is portrait, its proportion exactly 3 units wide to 4 units tall, with softly "    "rounded corners and genuinely worn, softened, bumped and slightly frayed aged edges. "    "It is heavyweight uncoated aged card stock photographed close up: clearly visible fibre "    "grain and tooth, a faint mottling of lighter and darker patches, a few tiny brown foxing "    "specks, and a soft darker vignette gathering toward the four edges and corners. Printed "    "on it, set well inside the edge, is one thin hairline rule frame in a darker warm brown, "    "whose four corners are cut at 45 degrees with a small ornamental bracket notch exactly as "    "in the reference, the printed line slightly broken and faded with age. THE CARD FACE IS "    "COMPLETELY BLANK: no object, no photograph, no picture, no icon, no text, no letters, no "    "numbers, no title, no caption, no horizontal rule, and nothing whatsoever printed inside "    "the hairline frame. An empty card face only. Even diffuse studio lighting, shot straight "    "down, perfectly parallel, sharp across the whole card, photographic not illustrated.")FACE_PROMPT = (    "Take the first image — a single blank aged collectible card lying on a magenta background "    "— and change ONE thing only: re-paper the card's face in {tint}, leaving a narrow pale "    "IVORY aged card-stock margin around the outside edge of the card so the panel sits inside "    "an ivory border, exactly like the second reference card. The colour must be FADED, DUSTY "    "and DESATURATED — the colour of a card that has sat in a drawer for fifty years, matching "    "the quiet muted tone of the second reference card; never bright, never saturated, never "    "vivid. The paper must stay PAPER: its fibre grain and tooth, its faint mottling, its few "    "small brown foxing specks and the soft darker vignette gathering toward its edges and "    "corners all clearly visible through the colour. Keep everything else identical: the same "    "card position, the same size, the same 3-by-4 proportion, the same rounded corners, the "    "same worn bumped edges, the same magenta background, the same thin hairline frame with "    "45-degree notched corners. The card face stays COMPLETELY BLANK — no object, no picture, "    "no text, no letters, no numbers, no rule, nothing printed inside the frame.")def generate(out: pathlib.Path, prompt: str, refs: list[pathlib.Path]) -> None:    command = [sys.executable, str(GEN), "--out", str(out), "--size", SIZE, "--quality", "high",               "--prompt", prompt]    for ref in refs:        command += ["--ref", str(ref)]    subprocess.run(command, check=True)def main() -> None:    parser = argparse.ArgumentParser()    parser.add_argument("--out-dir", required=True)    parser.add_argument("--anchor", required=True, help="ONE card cut out of the reference sheet")    parser.add_argument("--anchor-tinted", required=True, help="one TINTED card from the sheet")    parser.add_argument("--master", help="an already-generated master to edit (skips the first call)")    parser.add_argument("--only", action="append", default=[], choices=sorted(TINTS))    arguments = parser.parse_args()    out_dir = pathlib.Path(arguments.out_dir)    out_dir.mkdir(parents=True, exist_ok=True)    anchor = pathlib.Path(arguments.anchor)    tinted = pathlib.Path(arguments.anchor_tinted)    master = pathlib.Path(arguments.master) if arguments.master else out_dir / "master.png"    if not arguments.master:        print("master…")        generate(master, MASTER_PROMPT, [anchor])    for tint in (arguments.only or sorted(TINTS)):        print(f"{tint}…")        generate(out_dir / f"face-{tint}.png", FACE_PROMPT.format(tint=TINTS[tint]), [master, tinted])if __name__ == "__main__":    main()