"""Prepare and repack Mafuyu's generated interaction animation atlas. The generator authored a 4x4 presentation sheet whose row spacing is not perfectly uniform. This helper extracts clean horizontal action strips before the Game Studio normalizer aligns every frame to a shared bottom-center anchor. """ from __future__ import annotations import argparse from pathlib import Path from PIL import Image def frame_x_bounds(width: int, column: int) -> tuple[int, int]: return round(column * width / 4), round((column + 1) * width / 4) def make_strip(source: Image.Image, rows: list[tuple[int, int]], output: Path) -> None: slots: list[Image.Image] = [] slot_width = round(source.width / 4) strip_height = max(bottom - top for top, bottom in rows) for top, bottom in rows: for column in range(4): left, right = frame_x_bounds(source.width, column) slot = source.crop((left, top, right, bottom)) canvas = Image.new("RGBA", (slot_width, strip_height), (0, 0, 0, 0)) canvas.alpha_composite(slot, ((slot_width - slot.width) // 2, strip_height - slot.height)) slots.append(canvas) strip = Image.new("RGBA", (slot_width * len(slots), strip_height), (0, 0, 0, 0)) for index, slot in enumerate(slots): strip.alpha_composite(slot, (index * slot_width, 0)) output.parent.mkdir(parents=True, exist_ok=True) strip.save(output) def extract(source_path: Path, output_dir: Path) -> None: source = Image.open(source_path).convert("RGBA") make_strip(source, [(0, 330)], output_dir / "interact-strip.png") make_strip(source, [(330, 660)], output_dir / "sit-strip.png") make_strip(source, [(660, 940), (940, source.height)], output_dir / "rest-strip.png") def pack(frames_root: Path, output: Path, frame_size: int) -> None: atlas = Image.new("RGBA", (frame_size * 4, frame_size * 4), (0, 0, 0, 0)) rows = [ ("interact", range(1, 5)), ("sit", range(1, 5)), ("rest", range(1, 5)), ("rest", range(5, 9)), ] for row, (folder, indices) in enumerate(rows): for column, index in enumerate(indices): frame = Image.open(frames_root / folder / f"{index:02d}.png").convert("RGBA") atlas.alpha_composite(frame, (column * frame_size, row * frame_size)) output.parent.mkdir(parents=True, exist_ok=True) atlas.save(output) def main() -> None: parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest="command", required=True) extract_parser = subparsers.add_parser("extract") extract_parser.add_argument("--source", type=Path, required=True) extract_parser.add_argument("--out-dir", type=Path, required=True) pack_parser = subparsers.add_parser("pack") pack_parser.add_argument("--frames-root", type=Path, required=True) pack_parser.add_argument("--output", type=Path, required=True) pack_parser.add_argument("--frame-size", type=int, default=256) args = parser.parse_args() if args.command == "extract": extract(args.source, args.out_dir) else: pack(args.frames_root, args.output, args.frame_size) if __name__ == "__main__": main()