#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Mesen2's frame of every ROM in a folder, cropped to the visible lines. EMU_TOOLS_GPL=... python3 -I scripts/mesen_frames.py tests/test_roms/krom/PPU --frames 60 \ --out tests/detector_frames/krom-mesen60 Runs mesen2-runner (``$EMU_TOOLS_GPL/tools/mesen2/run.py --machine snes --region auto``, a GPL tool run as a separate program; ``scripts/emu_tools.py``) on each ``.sfc`` that has a ``.png`` beside it (or every one with ``--all``) and keeps lines 1-224 of the frame: Mesen2's 256x239 frame starts 7 rows above line 1 (14 in its 512x478 hi-res and interlace frames; validation.md, 2026-10-21). The output folder is gitignored (pictures of third-party ROMs); tests/test_milestone2_pictures.py reads it. Needs Pillow. Values are OBSERVED (Mesen2 b9fa69d via mesen2-runner 0.2.0). """ from __future__ import annotations import argparse import concurrent.futures import sys import tempfile from pathlib import Path from PIL import Image ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) # run with -I: the script directory is not on the path import emu_tools # noqa: E402 (finds mesen2-runner through EMU_TOOLS_GPL) def frame(rom: Path, frames: int, out: Path) -> str: with tempfile.TemporaryDirectory() as tmp: raw = emu_tools.mesen2_frames(rom, [frames], Path(tmp), region="auto")[frames] if raw is None: return f"no frame\t{rom.name}" image = Image.open(raw).convert("RGB") top, height = (14, 448) if image.height > 300 else (7, 224) image.crop((0, top, image.width, top + height)).save(out / f"{rom.stem}.png") return f"ok\t{rom.name}" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("folder", type=Path) parser.add_argument("--frames", type=int, default=60) parser.add_argument("--out", type=Path, required=True) parser.add_argument("--all", action="store_true", help="every ROM, not only those with a .png") parser.add_argument("--jobs", type=int, default=8) args = parser.parse_args() emu_tools.require(emu_tools.MESEN2_RUN) # exit 3, naming EMU_TOOLS_GPL, without it args.out.mkdir(parents=True, exist_ok=True) roms = sorted( p for p in args.folder.rglob("*.sfc") if args.all or p.with_suffix(".png").exists() ) with concurrent.futures.ThreadPoolExecutor(args.jobs) as pool: for line in pool.map(lambda r: frame(r, args.frames, args.out), roms): print(line, flush=True) return 0 if __name__ == "__main__": sys.exit(main())