Files
windroost/tools/pxl_to_runtime.py
T
wushenghua 54249da0ff art: H07 full-fidelity independent sprite + reusable full-art pipeline (H01 style)
Continued the full-art goal (owner: H01 fidelity, my call on priority). Proved
the end-to-end path from a single accepted AI portrait to a runtime 4-frame
atlas, and produced H07 at H01 fidelity.

- H07 base portrait generated with gpt-image-2.5 using H01 as identity/style
  anchor (base-source.png, 1254x1254, clean magenta). Download succeeded after
  diagnosing the real blocker: macOS system proxy 127.0.0.1:7897 corrupts the
  ZexAPI CDN; --proxy '' --noproxy '*' + --compressed + PIL-full-decode retry
  loop (tools/patient_download.sh) gets complete files reliably.
- idle strip repeatedly failed to download (random CDN truncation), so derived
  a 4-frame breathing loop from the accepted H01-fidelity portrait via
  tools/derive_idle_frames.py, packed with pack_animation_strip.py into a
  384x96 runtime atlas + manifest with provenance. H07 now loads and advances
  frames in runtime (BREATHING_PASS 15 stages).
- Reusable tools: ai_sprite_to_runtime.py (strip->runtime, verified against H02
  raw), derive_idle_frames.py, cdn_download.py, patient_download.sh,
  pxl_to_runtime.py (code-art fallback). Documented in PRODUCTION/50.
- breathing_test now asserts H07 idle too.

Honest note: the portrait is H01 hand-painted fidelity; the idle animation is
procedural micro-motion (not AI pose-generated) because the strip download was
unreliable. Full-game coverage: heroes H01-H07, R1 enemies, B01 done; R2-R5
enemies + H08-H54 + B02-B20 remain. All gates pass; P2_CHECKS_PASS.
prototype/ untracked.
2026-11-18 22:34:19 +08:00

97 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""Convert a pxl horizontal frame strip into the runtime atlas + manifest.
pxl exports a transparent strip (N cells side by side, e.g. 4x32 for a 32px
sprite). The Godot runtime expects a fixed-grid atlas of 96x96 cells with a
manifest describing each frame's atlas rect, cell offset, size and foot line.
This converter keeps it genuine pixel art: nearest-neighbour scaling only,
one shared scale + foot line across all frames (no per-frame bbox fit jitter).
"""
from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path
from PIL import Image
def main() -> int:
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--input", required=True, type=Path, help="pxl horizontal frame strip PNG")
ap.add_argument("--character", required=True)
ap.add_argument("--state", default="idle")
ap.add_argument("--frames", type=int, default=4)
ap.add_argument("--source-cell", type=int, default=32)
ap.add_argument("--cell", type=int, default=96)
ap.add_argument("--fps", type=int, default=8)
ap.add_argument("--loop", action="store_true", default=True)
ap.add_argument("--fit", type=float, default=0.86)
ap.add_argument("--output", required=True, type=Path, help="output atlas PNG")
ap.add_argument("--manifest", required=True, type=Path)
args = ap.parse_args()
src = Image.open(args.input).convert("RGBA")
fw, fh = args.source_cell, args.source_cell
assert src.width == fw * args.frames, f"strip width {src.width} != {fw}*{args.frames}"
assert src.height == fh, f"strip height {src.height} != {fh}"
# Shared scale so the tallest art fits the cell; one shared foot line.
scale = (args.cell * args.fit) / fh
out_cells = []
boxes = []
for i in range(args.frames):
frame = src.crop((i * fw, 0, (i + 1) * fw, fh))
alpha = frame.getchannel("A")
bbox = alpha.getbbox() or (0, 0, fw, fh)
# Place foot (bbox bottom) on a shared foot line near cell bottom.
foot_y = int(args.cell * 0.875)
bottom = int(bbox[3] * scale)
dx = (args.cell - int((bbox[2] - bbox[0]) * scale)) // 2 - int(bbox[0] * scale)
dy = foot_y - bottom
scaled = frame.resize((int(fw * scale), int(fh * scale)), Image.NEAREST)
cell = Image.new("RGBA", (args.cell, args.cell), (0, 0, 0, 0))
cell.paste(scaled, (dx, dy))
out_cells.append(cell)
boxes.append({"bbox": bbox, "dx": dx, "dy": dy})
atlas = Image.new("RGBA", (args.cell * args.frames, args.cell), (0, 0, 0, 0))
frames_meta = []
for i, cell in enumerate(out_cells):
atlas.paste(cell, (i * args.cell, 0))
alpha = cell.getchannel("A")
ab = alpha.getbbox() or (0, 0, args.cell, args.cell)
frames_meta.append({
"name": f"frame-{i}",
"atlas_rect": [i * args.cell, 0, args.cell, args.cell],
"cell_offset": [ab[0], ab[1]],
"size": [ab[2] - ab[0], ab[3] - ab[1]],
})
args.output.parent.mkdir(parents=True, exist_ok=True)
atlas.save(args.output)
digest = hashlib.sha256(args.output.read_bytes()).hexdigest()
manifest = {
"schema": 1, "status": "runtime",
"character_id": args.character, "state": args.state,
"frame_count": args.frames, "fps": args.fps, "loop": args.loop,
"cell_size": [args.cell, args.cell],
"atlas_size": [args.cell * args.frames, args.cell],
"logical_canvas": [args.cell, args.cell],
"fit": args.fit, "scale": round(scale, 6),
"foot_y": int(args.cell * 0.875),
"resample": "nearest",
"source_frames": [str(args.input)] * args.frames,
"atlas_sha256": digest,
"frames": frames_meta,
"generator": "tools/pxl_to_runtime.py",
}
args.manifest.parent.mkdir(parents=True, exist_ok=True)
args.manifest.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(f"atlas {args.output} {atlas.size}; manifest {args.manifest}; frames={args.frames}")
return 0
if __name__ == "__main__":
raise SystemExit(main())