From 436dee559c798c626f18916fd349ad930aec7d4e Mon Sep 17 00:00:00 2001 From: Mike Liu Date: Wed, 25 Feb 2026 00:16:09 +0000 Subject: [PATCH] =?UTF-8?q?ralph:=20story-4=20parse=5Frestyled.py=20?= =?UTF-8?q?=E2=80=94=20sprite=20extraction=20with=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.6 --- prd.json | 2 +- scripts/reskin/parse_restyled.py | 275 ++++++++++++++++++++++++++ scripts/reskin/test_parse_restyled.py | 105 ++++++++++ 3 files changed, 381 insertions(+), 1 deletion(-) create mode 100644 scripts/reskin/parse_restyled.py create mode 100644 scripts/reskin/test_parse_restyled.py diff --git a/prd.json b/prd.json index e65607cc29..3d4cd75a58 100644 --- a/prd.json +++ b/prd.json @@ -33,7 +33,7 @@ "title": "parse_restyled.py — sprite extraction with tests", "description": "Create scripts/reskin/parse_restyled.py adapted from /workspace/ottd-reskin-pipeline/parse_and_replace.py. Remove patch_sprite_sheets(), rebuild_grf(), save_png_small_idat(). Keep: extract_sprites_from_grid(), restore_alpha(), blend_tile_edges(), validate_sprite(). Add parse_restyled() main entry point that writes individual PNGs to output_dir. CLI: --manifest, --sprites-dir, --restyled-dir, --output-dir, --tiling, --no-tiling, --dry-run. Also create scripts/reskin/test_parse_restyled.py with 7 tests: test_restore_alpha, test_restore_alpha_transparent_original, test_blend_tile_edges, test_blend_tile_edges_tiny_sprite, test_validate_sprite_different, test_validate_sprite_too_similar, test_validate_sprite_transparent. See docs/plans/2026-02-25-reskin-pipeline.md Task 4 for exact code.", "verify": "cd /workspace/OpenRCT2-Web && python3 -m pytest scripts/reskin/test_parse_restyled.py -v", - "status": "pending", + "status": "passed", "retries": 0 }, { diff --git a/scripts/reskin/parse_restyled.py b/scripts/reskin/parse_restyled.py new file mode 100644 index 0000000000..b87025e682 --- /dev/null +++ b/scripts/reskin/parse_restyled.py @@ -0,0 +1,275 @@ +#!/usr/bin/env python3 +"""Extract restyled sprites from AI-generated grids. + +Reads the manifest and restyled grid images, extracts individual sprites +using exact inverse transforms, applies original alpha masks, optionally +blends tile edges, and saves restyled PNGs. + +Usage: + python3 parse_restyled.py --manifest batches/manifest.json --sprites-dir sprites/ + python3 parse_restyled.py --manifest batches/manifest.json --sprites-dir sprites/ --dry-run +""" + +import os +import json +import argparse +from PIL import Image +import numpy as np + + +def extract_sprites_from_grid(restyled_path, batch_meta, canvas_size): + """Extract individual sprites from a restyled grid image. + + Returns list of (original_filename, cropped_image, sprite_meta). + """ + img = Image.open(restyled_path).convert("RGBA") + img_w, img_h = img.size + + scale = batch_meta["scale"] + offset_x = batch_meta["offset_x"] + offset_y = batch_meta["offset_y"] + native_w = batch_meta["native_w"] + native_h = batch_meta["native_h"] + cell_w = batch_meta["cell_w"] + cell_h = batch_meta["cell_h"] + glw = 6 # grid line width + + # Scale offsets if AI returned a different resolution + img_scale = img_w / canvas_size + actual_offset_x = int(offset_x * img_scale) + actual_offset_y = int(offset_y * img_scale) + actual_w = int(native_w * scale * img_scale) + actual_h = int(native_h * scale * img_scale) + + # Crop the grid region from the canvas + grid_crop = img.crop(( + actual_offset_x, actual_offset_y, + actual_offset_x + actual_w, actual_offset_y + actual_h + )) + + # Scale back to native resolution + grid_native = grid_crop.resize((native_w, native_h), Image.LANCZOS) + + extracted = [] + for sprite_info in batch_meta["sprites"]: + row = sprite_info["cell_row"] + col = sprite_info["cell_col"] + orig_w = sprite_info["original_w"] + orig_h = sprite_info["original_h"] + + # Cell top-left in native coordinates + cx = glw + col * (cell_w + glw) + cy = glw + row * (cell_h + glw) + + # Sprite was centered in cell + paste_x = cx + (cell_w - orig_w) // 2 + paste_y = cy + (cell_h - orig_h) // 2 + + # Extract at exact original dimensions + sprite_crop = grid_native.crop(( + paste_x, paste_y, + paste_x + orig_w, paste_y + orig_h + )) + + extracted.append((sprite_info["original_file"], sprite_crop, sprite_info)) + + return extracted + + +def restore_alpha(restyled_sprite, original_path): + """Apply original sprite's alpha channel to the restyled sprite.""" + original = Image.open(original_path).convert("RGBA") + restyled = restyled_sprite.convert("RGBA") + + if restyled.size != original.size: + restyled = restyled.resize(original.size, Image.LANCZOS) + + r, g, b, _ = restyled.split() + _, _, _, original_alpha = original.split() + + return Image.merge("RGBA", (r, g, b, original_alpha)) + + +def blend_tile_edges(restyled, original, edge_pixels=6): + """Blend outer edge pixels back to original for seamless tiling.""" + res = np.array(restyled).astype(float) + orig = np.array(original).astype(float) + h, w = res.shape[:2] + edge_pixels = min(edge_pixels, h // 2, w // 2) + if edge_pixels <= 0: + return restyled + mask = np.ones((h, w), dtype=float) + for i in range(edge_pixels): + a = i / edge_pixels + mask[i, :] = np.minimum(mask[i, :], a) + mask[h - 1 - i, :] = np.minimum(mask[h - 1 - i, :], a) + mask[:, i] = np.minimum(mask[:, i], a) + mask[:, w - 1 - i] = np.minimum(mask[:, w - 1 - i], a) + m = mask[:, :, np.newaxis] + blended = res * m + orig * (1.0 - m) + return Image.fromarray(blended.astype(np.uint8)) + + +def validate_sprite(restyled, original_path): + """Check that the restyled sprite is reasonable. + + Returns (ok, message). + """ + original = Image.open(original_path).convert("RGBA") + + if restyled.size != original.size: + return False, f"Size mismatch: {restyled.size} vs {original.size}" + + orig_arr = np.array(original) + rest_arr = np.array(restyled) + orig_alpha = orig_arr[:, :, 3] + visible = orig_alpha > 0 + + if not visible.any(): + return True, "OK (fully transparent)" + + orig_rgb = orig_arr[:, :, :3].astype(float) + rest_rgb = rest_arr[:, :, :3].astype(float) + diff = np.abs(rest_rgb - orig_rgb) + mean_diff = diff[visible].mean() + + if mean_diff < 3: + return False, f"Too similar (diff={mean_diff:.1f})" + + return True, f"OK (diff={mean_diff:.1f})" + + +def parse_restyled(manifest_path, sprites_dir, restyled_dir=None, + output_dir=None, tiling=None, dry_run=False): + """Main entry point: extract restyled sprites from AI grids. + + Args: + manifest_path: Path to batches/manifest.json. + sprites_dir: Directory with original sprite PNGs (for alpha masks). + restyled_dir: Directory with restyled grid images + (default: sibling of batch dir named 'restyled'). + output_dir: Where to write restyled individual PNGs + (default: same as sprites_dir, overwriting originals). + tiling: Whether to blend tile edges. If None, reads from manifest. + dry_run: Validate only, don't write files. + + Returns dict with keys: total, skipped, missing_batches. + """ + with open(manifest_path) as f: + manifest = json.load(f) + + canvas_size = manifest["canvas_size"] + use_tiling = tiling if tiling is not None else manifest.get("tiling", False) + + batch_dir = os.path.dirname(manifest_path) + if restyled_dir is None: + restyled_dir = os.path.join(os.path.dirname(batch_dir), "restyled") + if output_dir is None: + output_dir = sprites_dir + + if not dry_run: + os.makedirs(output_dir, exist_ok=True) + + all_ok = [] + skipped = 0 + missing_batches = [] + + for batch_meta in manifest["batches"]: + batch_id = batch_meta["batch_id"] + restyled_path = os.path.join(restyled_dir, f"{batch_id}_restyled.png") + + if not os.path.exists(restyled_path): + print(f" {batch_id}: MISSING, skipping") + missing_batches.append(batch_id) + continue + + try: + extracted = extract_sprites_from_grid( + restyled_path, batch_meta, canvas_size + ) + except Exception as e: + print(f" {batch_id}: ERROR ({e}), skipping") + missing_batches.append(batch_id) + continue + + batch_ok = 0 + batch_warn = 0 + for filename, sprite_img, meta in extracted: + orig_path = os.path.join(sprites_dir, filename) + + # Restore original alpha mask + sprite_img = restore_alpha(sprite_img, orig_path) + + # Blend tile edges + if use_tiling: + original = Image.open(orig_path).convert("RGBA") + sprite_img = blend_tile_edges(sprite_img, original) + + # Validate + ok, msg = validate_sprite(sprite_img, orig_path) + if not ok: + print(f" WARNING: {filename}: {msg}") + batch_warn += 1 + skipped += 1 + continue + + if not dry_run: + sprite_img.save(os.path.join(output_dir, filename)) + + all_ok.append(filename) + batch_ok += 1 + + print(f" {batch_id}: {batch_ok} OK, {batch_warn} warnings") + + print(f"\nTotal: {len(all_ok)} sprites ready, {skipped} skipped, " + f"{len(missing_batches)} batches missing") + + if dry_run: + print("Dry run - no files written.") + + return { + "total": len(all_ok), + "skipped": skipped, + "missing_batches": missing_batches, + } + + +def main(): + parser = argparse.ArgumentParser( + description="Extract restyled sprites from AI grids" + ) + parser.add_argument("--manifest", required=True, + help="Path to manifest.json") + parser.add_argument("--sprites-dir", required=True, + help="Directory with original sprite PNGs") + parser.add_argument("--restyled-dir", default=None, + help="Directory with restyled grid images") + parser.add_argument("--output-dir", default=None, + help="Output directory for restyled PNGs " + "(default: overwrite sprites-dir)") + parser.add_argument("--tiling", action="store_true", + help="Blend tile edges for seamless tiling") + parser.add_argument("--no-tiling", action="store_true", + help="Disable tile edge blending even if manifest says tiling") + parser.add_argument("--dry-run", action="store_true", + help="Validate only, don't write files") + args = parser.parse_args() + + tiling = None + if args.tiling: + tiling = True + elif args.no_tiling: + tiling = False + + parse_restyled( + manifest_path=args.manifest, + sprites_dir=args.sprites_dir, + restyled_dir=args.restyled_dir, + output_dir=args.output_dir, + tiling=tiling, + dry_run=args.dry_run, + ) + + +if __name__ == "__main__": + main() diff --git a/scripts/reskin/test_parse_restyled.py b/scripts/reskin/test_parse_restyled.py new file mode 100644 index 0000000000..6cf27db005 --- /dev/null +++ b/scripts/reskin/test_parse_restyled.py @@ -0,0 +1,105 @@ +"""Tests for parse_restyled.py.""" + +import json +import os +import tempfile +import numpy as np +from PIL import Image + +from parse_restyled import ( + restore_alpha, + blend_tile_edges, + validate_sprite, + extract_sprites_from_grid, +) + + +def _make_sprite(w, h, color=(255, 0, 0, 200)): + return Image.new("RGBA", (w, h), color) + + +def test_restore_alpha(): + """RGB from restyled, alpha from original.""" + with tempfile.TemporaryDirectory() as d: + orig = Image.new("RGBA", (10, 10), (0, 0, 0, 128)) + orig_path = os.path.join(d, "orig.png") + orig.save(orig_path) + + restyled = Image.new("RGBA", (10, 10), (255, 0, 0, 255)) + result = restore_alpha(restyled, orig_path) + + arr = np.array(result) + assert arr[0, 0, 0] == 255 # red from restyled + assert arr[0, 0, 3] == 128 # alpha from original + + +def test_restore_alpha_transparent_original(): + """Fully transparent original stays transparent.""" + with tempfile.TemporaryDirectory() as d: + orig = Image.new("RGBA", (10, 10), (0, 0, 0, 0)) + orig_path = os.path.join(d, "orig.png") + orig.save(orig_path) + + restyled = Image.new("RGBA", (10, 10), (255, 0, 0, 255)) + result = restore_alpha(restyled, orig_path) + + arr = np.array(result) + assert (arr[:, :, 3] == 0).all() + + +def test_blend_tile_edges(): + """Edges should blend toward original.""" + restyled = Image.new("RGBA", (20, 20), (255, 0, 0, 255)) + original = Image.new("RGBA", (20, 20), (0, 0, 255, 255)) + result = blend_tile_edges(restyled, original, edge_pixels=4) + arr = np.array(result) + # Corner (0,0) should be close to original (blue) + assert arr[0, 0, 2] > arr[0, 0, 0] # more blue than red + # Center should be close to restyled (red) + assert arr[10, 10, 0] > arr[10, 10, 2] # more red than blue + + +def test_blend_tile_edges_tiny_sprite(): + """Tiny sprites shouldn't crash.""" + restyled = Image.new("RGBA", (4, 4), (255, 0, 0, 255)) + original = Image.new("RGBA", (4, 4), (0, 0, 255, 255)) + result = blend_tile_edges(restyled, original, edge_pixels=6) + assert result.size == (4, 4) + + +def test_validate_sprite_different(): + """Sufficiently different sprite passes validation.""" + with tempfile.TemporaryDirectory() as d: + orig = Image.new("RGBA", (10, 10), (0, 0, 0, 255)) + orig_path = os.path.join(d, "orig.png") + orig.save(orig_path) + + restyled = Image.new("RGBA", (10, 10), (255, 0, 0, 255)) + ok, msg = validate_sprite(restyled, orig_path) + assert ok + assert "OK" in msg + + +def test_validate_sprite_too_similar(): + """Nearly identical sprite fails validation.""" + with tempfile.TemporaryDirectory() as d: + orig = Image.new("RGBA", (10, 10), (100, 100, 100, 255)) + orig_path = os.path.join(d, "orig.png") + orig.save(orig_path) + + restyled = Image.new("RGBA", (10, 10), (101, 100, 100, 255)) + ok, msg = validate_sprite(restyled, orig_path) + assert not ok + assert "Too similar" in msg + + +def test_validate_sprite_transparent(): + """Fully transparent sprite passes.""" + with tempfile.TemporaryDirectory() as d: + orig = Image.new("RGBA", (10, 10), (0, 0, 0, 0)) + orig_path = os.path.join(d, "orig.png") + orig.save(orig_path) + + restyled = Image.new("RGBA", (10, 10), (255, 0, 0, 0)) + ok, msg = validate_sprite(restyled, orig_path) + assert ok