"""Which cells of an emulator frame differ from an expected frame, and how.""" from __future__ import annotations import sys from pathlib import Path import numpy as np from PIL import Image TILE = 8 def load(p: Path) -> np.ndarray: return np.asarray(Image.open(p).convert("RGB")) def main(actual: Path, expected: Path, *others: Path) -> None: a, e = load(actual), load(expected) others_rgb = [load(p) for p in others] cy_n, cx_n = a.shape[0] // TILE, a.shape[1] // TILE bad = [] for cy in range(cy_n): for cx in range(cx_n): ys, xs = slice(cy * TILE, (cy + 1) * TILE), slice(cx * TILE, (cx + 1) * TILE) if np.any(a[ys, xs] != e[ys, xs]): # does the emulator's cell match the same cell in some other expected frame? match = next((i for i, o in enumerate(others_rgb) if np.array_equal(a[ys, xs], o[ys, xs])), None) bad.append((cy, cx, match)) print(f"{len(bad)} of {cy_n * cx_n} cells differ") by_match: dict = {} for cy, cx, m in bad: by_match.setdefault(m, []).append((cy, cx)) for m, cells in sorted(by_match.items(), key=lambda kv: (kv[0] is None, kv[0])): label = "match NO other frame" if m is None else f"match other frame #{m} ({others[m].name})" print(f" {len(cells):4d} cells {label}: {cells[:12]}{' ...' if len(cells) > 12 else ''}") if __name__ == "__main__": main(*(Path(p) for p in sys.argv[1:]))