Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
docs: each shared judgment names its callers where its rule is written - the alpha verdict in gen.md, the tile join in asset-tools.md, the matte's lone pixels (a step the YCbCr list was missing) in chroma-alpha.md; pixel-draw.md's gates point there and a table says what the verb takes from sprite-gen and what is its own, and why
Soohong Kimd2752cf1556e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8e2866d..a862d6c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,7 +4,7 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the## Unreleased-- **New (experimental): `sprite-gen pixel-draw` — Claude Opus draws pixel art of 8–48 px directly, as a pixel grid.** The model writes a palette and one line of keys per pixel row (a character grid, or JSON with `--format json`); code checks it and renders it. `--subject` (or `--spec`), `--size` 8–48 (32), `--kind sprite|icon|tile`, `--frames` (up to four, drawn in one call; `--walk-cycle` for contact, passing, contact, passing), `--palette free[:N]|pico8|#hex,…|file`, `--candidates` (2), `--effort medium|high|xhigh` (high), `--batch` (Message Batches, half price), `--max-tokens` (32000 for one frame, 64000 for more), `--pick vision|human`. G1 reads every answer strictly; a row one key too long or short, a key outside the palette or rows missing at the end go back to the model on their own with a column ruler and are spliced in (two rounds), and an answer still broken — or broken beyond its rows — is drawn once more; a single frame written without its `FRAME` line is recovered and recorded. A sprite or icon is drawn to the engine's safe area — `prepare`'s margin, 9.4% of the side, floored, taken from `prepare` rather than copied — as a target for the body's average size, not a wall: at 32 px the body is about the size of the central 26×26 (16 → 14, 24 → 20, 48 → 40), a pose or a detail that needs the room (a raised weapon, a long blade, an effect, a wide stance) may reach past it into the margin, up to the canvas edge, and a sprite's feet stand on the safe area's bottom row, the row `prepare`'s fit grounds a frame on, in every frame; the prompt says it in rows and columns counted from 1. G2 palette, G3 alpha and G6 frames (one ground line, heights within a pixel, no two frames the same — a sprite's or icon's measured inside the safe area, so a staff or a spark that reaches into the margin in one frame is not a taller figure) fail a candidate; G4 tile seam, G5 stray pixels and a walk whose legs barely change sides warn. G8 safe area never fails a candidate: it takes the judgment the pixel snap already makes — one function, `safe_area_fit` in `frames/extract.py`, lifted out of the snap with its lines unchanged — on each frame's opaque box: past the physical cap (the canvas's width, its height less the margin under the floor row, 32×29 at 32 px) warns, a reroll candidate; past the safe area but within the cap is an info line, `info` in the report, which asks for nothing; and a sprite whose feet are off the floor row warns. Of two or more candidates that pass, one vision call at effort low picks (G7). An answer that spent every token thinking is drawn once more one effort lower; a refusal is reported as it came (`refused by the provider's content policy (stop_reason=refusal category=…)`); nothing falls back to another model. The run folder holds `grid.txt` (the truth: edit it and re-render with `--grid`), `palette.json`, `frame-<name>.png` written by an encoder whose bytes depend on the pixels alone, `preview-x<s>.png`, `tiled-3x3.png` for a tile, `anim.gif` for several frames, every call under `calls/` and `report.json` with each call's model, effort, usage (thinking tokens included), price, seconds and request id, the gate results and the repair history. A batch is recorded the moment it is submitted, and `--resume` collects it — and reads every answered call back — instead of buying it twice. It runs on `ANTHROPIC_API_KEY`, billed per call and said so on stderr; the transport is urllib, so the dependency surface stays Pillow and NumPy — [docs/pixel-draw.md](docs/pixel-draw.md).+- **New (experimental): `sprite-gen pixel-draw` — Claude Opus draws pixel art of 8–48 px directly, as a pixel grid.** The model writes a palette and one line of keys per pixel row (a character grid, or JSON with `--format json`); code checks it and renders it. `--subject` (or `--spec`), `--size` 8–48 (32), `--kind sprite|icon|tile`, `--frames` (up to four, drawn in one call; `--walk-cycle` for contact, passing, contact, passing), `--palette free[:N]|pico8|#hex,…|file`, `--candidates` (2), `--effort medium|high|xhigh` (high), `--batch` (Message Batches, half price), `--max-tokens` (32000 for one frame, 64000 for more), `--pick vision|human`. G1 reads every answer strictly; a row one key too long or short, a key outside the palette or rows missing at the end go back to the model on their own with a column ruler and are spliced in (two rounds), and an answer still broken — or broken beyond its rows — is drawn once more; a single frame written without its `FRAME` line is recovered and recorded. A sprite or icon is drawn to the engine's safe area — `prepare`'s margin, 9.4% of the side, floored, taken from `prepare` rather than copied — as a target for the body's average size, not a wall: at 32 px the body is about the size of the central 26×26 (16 → 14, 24 → 20, 48 → 40), a pose or a detail that needs the room (a raised weapon, a long blade, an effect, a wide stance) may reach past it into the margin, up to the canvas edge, and a sprite's feet stand on the safe area's bottom row, the row `prepare`'s fit grounds a frame on, in every frame; the prompt says it in rows and columns counted from 1. G2 palette, G3 alpha and G6 frames (one ground line, heights within a pixel, no two frames the same — a sprite's or icon's measured inside the safe area, so a staff or a spark that reaches into the margin in one frame is not a taller figure) fail a candidate; G4 tile seam, G5 stray pixels and a walk whose legs barely change sides warn. G3, G4 and G5 ask judgments sprite-gen already makes rather than keep their own, each lifted out of its owner unchanged: G3 is whether a frame's background is really transparent by the verdict `gen` reads a generated raw by (`chroma.alpha_verdict`, out of `classify_raw_alpha`: at least 5 % of the pixels and half the one-pixel border alpha 0 for a sprite or icon, with something drawn and the four corners clear; no alpha-0 pixel for a tile); G4 is `background-tile`'s join measure (`inspect_tile`), asked of a tile along x and along y, and warns with its reasons where it says `needs-review`; G5 counts the lone pixels the YCbCr matte cleanup erases (`frames.extract.isolated_dots`, out of `_cleanup_alpha_ycc`: no opaque pixel among the eight neighbours), so each pixel of a one-pixel diagonal stroke, which touches the next at a corner, is no longer a stray pixel. G8 safe area never fails a candidate: it takes the judgment the pixel snap already makes — one function, `safe_area_fit` in `frames/extract.py`, lifted out of the snap with its lines unchanged — on each frame's opaque box: past the physical cap (the canvas's width, its height less the margin under the floor row, 32×29 at 32 px) warns, a reroll candidate; past the safe area but within the cap is an info line, `info` in the report, which asks for nothing; and a sprite whose feet are off the floor row warns. Of two or more candidates that pass, one vision call at effort low picks (G7). An answer that spent every token thinking is drawn once more one effort lower; a refusal is reported as it came (`refused by the provider's content policy (stop_reason=refusal category=…)`); nothing falls back to another model. The run folder holds `grid.txt` (the truth: edit it and re-render with `--grid`), `palette.json`, `frame-<name>.png` written by an encoder whose bytes depend on the pixels alone, `preview-x<s>.png`, `tiled-3x3.png` for a tile, `anim.gif` for several frames, every call under `calls/` and `report.json` with each call's model, effort, usage (thinking tokens included), price, seconds and request id, the gate results and the repair history. A batch is recorded the moment it is submitted, and `--resume` collects it — and reads every answered call back — instead of buying it twice. It runs on `ANTHROPIC_API_KEY`, billed per call and said so on stderr; the transport is urllib, so the dependency surface stays Pillow and NumPy — [docs/pixel-draw.md](docs/pixel-draw.md).- **Fix: a poll xAI answers 429 or 5xx asks the same request again — the clip is bought once.** `video`, `video-extend` and `video-edit` ended the run on any non-2xx poll, after xAI had accepted the request and may bill it, and the line read `(HTTP 429)` — what `video-set`, and any caller that backs off on a refused submission, answers by submitting the clip again: a second clip paid for, the first never published. A poll answered `429`, `500`, `502`, `503` or `504` is now asked again on the same `request_id`, after a wait that doubles from 8 s up to 60 s while the polls are refused and goes back to 4 s on an answer; a stderr note says each, the status in plain digits. The poll timeout (`SPRITE_GEN_VIDEO_TIMEOUT_SECONDS`) still bounds the whole poll, a pending poll's waits included (and, below, the downloads). A refused POST is still never sent again, and a `400` or `404` poll still ends the run with its status (a `401` or `403` poll is unfinished, below).- **Output change: a generation the poll timeout cut short is unfinished, named by its `request_id`.** Polls still pending or still refused at the timeout end the run with a line that names `request_id=<id>`, says the generation may still finish and be billed, and carries no `(HTTP <n>)`, a refusal line's status — a caller that reads `(HTTP 429)` there sends the request again, unbilled only when it was refused. The pending line keeps its front (`still 'pending' after N polls — poll timeout;`). With `--report` the run leaves a `sprite-gen-video-unfinished` report there (`verb`, `auth_source`, `model`, `request_id`, `out`, `polls`, `polls_refused`, `last_poll`, `elapsed_seconds`) and prints nothing on stdout; Python callers catch `GenerationUnfinished`, a `SystemExit`. The success report gains `polls_refused`, how many of its `polls` xAI answered 429 or 5xx (docs/video.md, An unfinished generation).- **`tests/gen/test_video.py`** scripts the polls: each of the five statuses refused twice, then pending, then done, is asked on the one request with waits 8, 16, 4 and published with `polls_refused` 2; polls refused until a 300 s timeout wait 8, 16, 32, 60 × 4, ask no poll whose wait would reach the timeout and end unfinished after eight with no `(HTTP n)` and no `HTTP 429` on stderr; a pending timeout names the request; a 400 or 404 poll ends at once; a refused POST is sent once; the CLI leaves the unfinished report and an empty stdout. The 500-poll case of the failure table is a 404 now, the status that still ends a run.diff --git a/docs/asset-tools.md b/docs/asset-tools.mdindex 17fde0a..6251458 100644--- a/docs/asset-tools.md+++ b/docs/asset-tools.md@@ -29,7 +29,7 @@ sprite-gen background-tile --source frontage.png --axis x --period 512 --overlapThe report measures the join against ordinary interior differences, including alpha and colour over light/dark backgrounds. It records the selected seam and source crop. A numerical join score does not prove that architecture or silhouettes repeat convincingly: inspect at least two adjacent copies. The tool does not invent missing roofs, perspective or scenery.-Python APIs: `make_tile(image, period, *, axis="x", overlap=32)` and `inspect_tile(image, axis="x")` in `sprite_gen.background.tile`. The former returns the image and report; the latter only measures.+Python APIs: `make_tile(image, period, *, axis="x", overlap=32)` and `inspect_tile(image, axis="x")` in `sprite_gen.background.tile`. The former returns the image and report; the latter only measures. `inspect_tile` is the one measure of a tile's join: [`pixel-draw`](pixel-draw.md#gates)'s G4 tile seam asks it of a drawn tile along x and along y and warns where it says `needs-review`.## Shared asset inputdiff --git a/docs/chroma-alpha.md b/docs/chroma-alpha.mdindex 37f8174..bd63628 100644--- a/docs/chroma-alpha.md+++ b/docs/chroma-alpha.md@@ -75,6 +75,7 @@ Port of perfectpixel-studio's `chroma.go` (MIT, see NOTICE). The default RGB pat3. **Despill** — subtracts only the key-direction chroma component inside the despill band; colors orthogonal to the key keep their saturation.4. **Border flood fill** — 4-connected from the image borders through lenient key-chroma pixels; interior key-family pixels (gems, highlights) never connect and survive.5. **Self-diagnostic rematte** — an opaque-fraction spike (subject erased) or declared-key residue spike (background survived) triggers a rematte with the declared pure key; the better result wins and the fallback is reported via extraction warnings (never silent).+6. **Dots and pinholes** — on the matte that won, a lone pixel (opaque, with no opaque pixel among its eight neighbours: `isolated_dots`) is erased and a clear pixel with seven or more opaque neighbours is filled; soft edges are left as they are. A pixel that touches another only at a corner is joined to it, as in every component the engine reads. `isolated_dots` is the one judgment of a lone pixel: [`pixel-draw`](pixel-draw.md#gates)'s G5 counts a drawn grid's stray pixels by it.Select with request `chroma.mode: "ycbcr"` or `--chroma-mode ycbcr`. **The default stays `"rgb"`**: on clean flat-key raws the RGB path's exact-solve unmix already removes key tint completely, while ycbcr's fixed-scale despill leaves a small tinted soft-edge halo — ycbcr is for degraded sources (shaded/gradient backgrounds, JPEG chroma noise), not a general upgrade.diff --git a/docs/gen.md b/docs/gen.mdindex a0eae18..876e7f7 100644--- a/docs/gen.md+++ b/docs/gen.md@@ -325,7 +325,9 @@ strategy it can execute, and `--alpha-mode auto` (the default) follows it:refused after the paid call: `alpha.decontam` records `{"requested": …, "skipped":…}` and stderr says so (a warning for `palette`, which demands the pass). The checkis the counts, not the rounded percentages: a raw with a single alpha-0 pixel is- `ambiguous`, reported as `0.0`.+ `ambiguous`, reported as `0.0`. The judgment of the alpha band is one function,+ `chroma.alpha_verdict`: [`pixel-draw`](pixel-draw.md#gates)'s G3 asks it of every frame+ it drew (a sprite or icon frame must be `real-alpha`, a tile `no-alpha`).Measured 2026-10-04 (codex `image_gen`, subscription, one transparent front still asthe reference, three takes per arm): with the step down's prompt (no transparencyrequest, no key colour), a transparent (RGBA) reference came back with real alphadiff --git a/docs/pixel-draw.md b/docs/pixel-draw.mdindex dc69e7c..e1582af 100644--- a/docs/pixel-draw.md+++ b/docs/pixel-draw.md@@ -4,7 +4,7 @@> varies from run to run: expect to compare candidates, re-draw, or touch up `grid.txt` by hand. The numbers below come from a> small set of measured runs, not from a benchmark.-> Owns: `sprite-gen pixel-draw`: the inputs, the safe area of a sprite or icon, the run folder and `report.json`, gates G1–G8, the row repair, the one redraw, the pick between candidates, Message Batches and `--resume`, re-rendering an edited `grid.txt` · Index: [docs/README.md](README.md)+> Owns: `sprite-gen pixel-draw`: the inputs, the safe area of a sprite or icon, the run folder and `report.json`, gates G1–G8 and what they take from sprite-gen, the row repair, the one redraw, the pick between candidates, Message Batches and `--resume`, re-rendering an edited `grid.txt` · Index: [docs/README.md](README.md)The model does not paint an image that is then shrunk to a grid. It writes the grid itself: apalette of keys and, per frame, one line of keys per pixel row. Code checks every answer, asks for@@ -102,7 +102,8 @@ For each candidate, independently (in parallel, or in one batch):(xhigh → high → medium → low). A candidate is drawn at most twice in all.4. **G2–G6 and G8** on the pixels. G2, G3 and G6 fail the candidate; G4, G5, the walk check and G8(a frame past the physical cap, feet off the floor row) warn; G8 also notes a frame past the safe- area within the cap in an info line, which asks for nothing.+ area within the cap in an info line, which asks for nothing. G3, G4, G5 and G8 are judgments+ sprite-gen already makes elsewhere, asked rather than copied ([below](#what-it-takes-from-sprite-gen)).5. **G7 pick.** Of two or more candidates that passed, one vision call (`claude-opus-5-5`, effortlow, structured output) looks at all of them on one sheet, shuffled under letters, scores each andnames the best; with `--pick human` nobody picks and the candidates wait for a person. One that@@ -162,13 +163,39 @@ past the safe area has not been measured yet.|---|---|---|| G1 parse | One block; palette lines `<key> #RRGGBB` (`.` is transparent and nothing else); the frames asked for, each exactly N rows of N palette keys | Row issues → row repair (two rounds), then one redraw; anything else → one redraw. Recovered by code and recorded: a single frame written without its `FRAME` line or under another name, frames in another order, the fence tagged otherwise, no `END` line || G2 palette | Opaque colours used ≤ the limit; a fixed palette's colours only | fails |-| G3 alpha | Sprite/icon: something drawn, four transparent corners, border at least half transparent. Tile: every pixel opaque | fails |-| G4 tile seam | Colour step across the wrap edges ÷ the mean step inside the tile ≤ 1.5 | warns — mortar lines that meet the edge raise it on tiles that repeat cleanly; look at `tiled-3x3.png` |-| G5 stray pixels | Opaque pixels of a sprite/icon with no opaque 4-neighbour | warns |+| G3 alpha | Each frame's background by the judgment `gen` reads a generated raw by (`alpha_verdict`, [gen.md](gen.md#transparent-output--strategy-per-provider)): `real-alpha` is at least 5 % of the pixels and half the one-pixel border alpha 0, `no-alpha` none. Sprite/icon: `real-alpha`, something drawn, four transparent corners. Tile: `no-alpha` | fails |+| G4 tile seam | Each tile frame's join along x and along y, by the measure `background-tile` reports (`inspect_tile`, [asset-tools.md](asset-tools.md#repeating-tiles)): the wrap edge against the tile's own neighbours | warns where it says `needs-review`, with its reasons — a local measure, never proof that the tile repeats cleanly; look at `tiled-3x3.png` |+| G5 stray pixels | Lone pixels of a sprite/icon by the judgment the matte cleanup erases them by (`isolated_dots`, [chroma-alpha.md](chroma-alpha.md#chromamode-ycbcr--chrominance-plane-matting-opt-in)): opaque, with no opaque pixel among their eight neighbours — a pixel touching another only at a corner, as in a diagonal stroke, is joined | warns || G6 frames | Same bottom row in every frame, heights within one pixel, no two frames the same; a sprite's or icon's rows are measured inside the [safe area](#the-safe-area), so a staff raised or a spark into the margin in one frame is not a taller figure (the margin is G8's). With `--walk-cycle`: the contact frames differ by at least 40% of the mean contact↔passing difference | fails; the walk check warns ("weak leg alternation — touch up the legs") || G8 safe area | Sprite/icon: each frame's opaque box against the [safe area](#the-safe-area) and the physical cap, by the pixel snap's own judgment (`safe_area_fit`, [pixel-unfake.md](pixel-unfake.md#pixel_unfake-mode)); a sprite's body box ending on the floor row | never fails: past the physical cap, and feet off the floor row, warn; past the safe area within the cap is an info line (`info`), which asks for nothing. Below 11 px the margin is 0: the safe area and the cap are the canvas || G7 pick | Two or more passed: the vision pick (or a person) | a pick that fails ends the run `pick_failed`, candidates kept |+## What it takes from sprite-gen++`pixel-draw` is a new way to make a picture, not a new set of rules for one. Where sprite-gen already+answers a question the verb asks, it asks that function; a part that is its own says why.++| Part | Taken from | Owner of the rule |+|---|---|---|+| The safe area's numbers (margin, floor row) | `prepare`'s cell normalisation (`gen.prepare.normalize_cell`) | [run-contract.md](run-contract.md) |+| G3 transparent background | `gen.chroma.alpha_verdict`, which `classify_raw_alpha` reads a raw by | [gen.md](gen.md#transparent-output--strategy-per-provider) |+| G4 tile seam | `background.tile.inspect_tile`, `background-tile`'s join measure | [asset-tools.md](asset-tools.md#repeating-tiles) |+| G5 stray pixels | `frames.extract.isolated_dots`, the YCbCr matte cleanup's lone pixels | [chroma-alpha.md](chroma-alpha.md#chromamode-ycbcr--chrominance-plane-matting-opt-in) |+| G8 safe area | `frames.extract.safe_area_fit`, the pixel snap's judgment | [pixel-unfake.md](pixel-unfake.md#pixel_unfake-mode) |+| `anim.gif` | `util.gif_utils.save_clean_gif` | — |+| The pick sheet's checker | `qa.preview.checker`, the contact sheets' | — |+| The billing line, the printed report | `gen.base.announce_api_billing`, `gen.base.print_json` | [gen.md](gen.md) |+| The refusal line | `gen.refusal` (`CONTENT_POLICY`) | [gen.md](gen.md#direct-openai-calls-and-authentication) |+| What ends a call unanswered, and its words | `gen.xai` (`NO_ANSWER`, `no_answer_reason`) | — |++Its own, because nothing in sprite-gen does it: the grid and its parse (G1), the row repair, the palette+rules (`--palette`), `frame-<name>.png`'s encoder (its bytes depend on the pixels alone, which Pillow's+do not), G2's count against the rule, G6 (one pixel of height across a drawn set — `extract`'s size+check is for a frame the matte broke, under 35 % or over 275 % of the median frame's area), the walk check (the+video path's legs read a filmed cycle's steps, not whether two drawn contact frames swap legs), the+3×3 tiling, the vision pick, Message Batches and `--resume` (`video-resume` asks xAI again by request+id; a batch is collected, and every answered call read back), and the cost in `report.json`.+## The run folder```