Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(video-cycle-align): a kept frame is taken only under what its RIFE runs from now
Soohong Kim7c23df765cd2
diff --git a/sprite_gen/video/align.py b/sprite_gen/video/align.pyindex c265182..2f41d99 100644--- a/sprite_gen/video/align.py+++ b/sprite_gen/video/align.py@@ -799,8 +799,9 @@ def align_set(loop_dirs: list[Path], *, length: int | None = None, interpolate:`between_cache` is a directory every frame made between two source frames is kept in and taken from(`rife.Cache`), for a caller that aligns the same set again: a frame is taken only for the same twosource frames, fraction and interpolator (its `identity()`, which an injected one must have). The- report's `between_cache` counts the frames taken (`hits`), made (`misses`) and kept but unreadable;- it is None without a directory, and nothing is kept."""+ report's `between_cache` counts the frames taken (`hits`), made (`misses`), kept but unreadable, and+ made while what they are made from changed, so not kept (`not_kept`); it is None without a+ directory, and nothing is kept."""if len(loop_dirs) < 1:raise SystemExit("video-cycle-align: at least one --loop-dir")if between_cache is not None and interpolate is not None and not callable(getattr(interpolate, "identity", None)):diff --git a/sprite_gen/video/rife.py b/sprite_gen/video/rife.pyindex 312a565..a8af2d3 100644--- a/sprite_gen/video/rife.py+++ b/sprite_gen/video/rife.py@@ -29,8 +29,9 @@ The interpolator is a plain callable `(a, b, t) -> frame`, so a caller can be haA frame made between two frames can be kept and taken again (`Cache`): an alignment run twice onthe same source frames — again with the feet told — makes the same frames a second time, two RIFEcalls each (section 1 of docs/loop-repair.md has their cost; section 4 the cache). It is kept under-everything it is made from, the interpolator's `identity()` included, so it is taken only where-making it again would give the same bytes.+everything it is made from, the interpolator's `identity()` included — read again for every frame,+as the files a RIFE runs from can be written over under it — so it is taken only where making it+again would give the same bytes."""from __future__ import annotations@@ -83,6 +84,10 @@ CALL_TIMEOUT_SECONDS = 120# name, so no frame kept the old way is taken — and the PNG text a kept frame names its own pixels' sha256 under.CACHE_FORMAT = "sprite-gen frame between, 1"PIXELS_TEXT = "sprite-gen-pixels-sha256"+# The engine code that makes a frame between, as this process loaded it (`Rife.identity`): read once, at import —+# rife.py written over on disk later changes no code that runs here, so a frame made here is kept under the code that+# made it, and a process that loads other code makes it again.+CODE_SHA256 = hashlib.sha256(Path(__file__).read_bytes()).hexdigest()Interpolate = Callable[[Image.Image, Image.Image, float], Image.Image]@@ -343,7 +348,6 @@ class Rife:binary, model = locate()self.binary, self.model = binary, modelself.made = 0- self._identity: str | None = Nonedef __call__(self, a: Image.Image, b: Image.Image, t: float) -> Image.Image:with tempfile.TemporaryDirectory(prefix="sprite-gen-rife-") as td:@@ -356,17 +360,22 @@ class Rife:def identity(self) -> str:"""What a frame this RIFE makes depends on besides its two frames and t, as one string (`Cache` keys- on it): the binary's and the model's files by sha256, this module's code by sha256 (it turns a frame- into RIFE's two runs and back), NumPy's and Pillow's versions, and the machine — the Apple GPU and- llvmpipe are only measured to agree to the third decimal of an error (docs/loop-repair.md section 1),- so a frame made on one is not taken on another."""- if self._identity is None:+ on it), read as it is now on every call — nothing of it is kept, so a binary, model or machine changed+ under this very RIFE is another identity: the binary's and each of the model's files by sha256, and the+ model's path as the binary is handed it (the binary reads the model's version from its name); the engine+ code this process loaded (`CODE_SHA256`: it turns a frame into RIFE's two runs and back) and NumPy's and+ Pillow's versions; and the machine — the OS, and `uname`'s host name, OS release and build and+ architecture (platform.uname() keeps its first answer) — as the Apple GPU and llvmpipe are only measured+ to agree to the third decimal of an error (docs/loop-repair.md section 1), so a frame made on one is not+ taken on another. Files that cannot be read are a RIFE that fails (`RifeUnavailable`)."""+ try:model = sorted(p for p in self.model.iterdir() if p.is_file())- self._identity = json.dumps({- "binary": _file_sha256(self.binary), "model": {p.name: _file_sha256(p) for p in model},- "code": _file_sha256(Path(__file__)), "numpy": np.__version__, "pillow": PIL.__version__,- "machine": [platform.node(), platform.machine(), sys.platform]}, sort_keys=True)- return self._identity+ files = {"binary": _file_sha256(self.binary), "model": str(self.model), "model_files": {p.name: _file_sha256(p) for p in model}}+ except OSError as exc:+ raise RifeUnavailable(f"{self.binary.name}: the binary or model it runs from could not be read: {exc}") from exc+ return json.dumps({+ **files, "code": CODE_SHA256, "numpy": np.__version__, "pillow": PIL.__version__,+ "machine": [sys.platform, *(os.uname() if hasattr(os, "uname") else platform.uname()[:5])]}, sort_keys=True)def _kept(path: Path, size: tuple[int, int]) -> Image.Image | None:@@ -391,10 +400,14 @@ class Cache:A frame is kept under the sha256 of everything it is made from: CACHE_FORMAT, the interpolator's`identity()`, each of the two frames' mode, size and bytes, and `t` exactly. Another frame, fraction,- RIFE or machine is another key, so a frame kept for other inputs is never taken. A kept frame is a- PNG naming its own pixels' sha256, written whole or not at all; a file that does not read back as- that frame (cut short, written over) is made again and kept over it, counted `unreadable`. A frame- taken is a new image of the kept pixels, as the interpolator's own frame is.+ RIFE or machine is another key, so a frame kept for other inputs is never taken. The key is read+ for every frame asked for, and once more after a frame is made: a frame made while what it is made+ from changed (a binary or model written over between RIFE's two runs) is handed on as made but not+ kept, as no key names what made it, counted `not_kept`. Frames are kept for RGBA frames only: their+ mode, size and bytes are all of them, where a palette or a transparent colour is not in an image's+ bytes. A kept frame is a PNG naming its own pixels' sha256, written whole or not at all; a file that+ does not read back as that frame (cut short, written over) is made again and kept over it, counted+ `unreadable`. A frame taken is a new image of the kept pixels, as the interpolator's own frame is.Nothing is kept unless a caller names the directory, and nothing here removes it: it belongs to thecaller, who gives each alignment its own. It is created readable by this user only. Counts the frames@@ -403,9 +416,13 @@ class Cache:def __init__(self, directory: Path):self.directory = Path(directory).expanduser().resolve()self.directory.mkdir(mode=0o700, parents=True, exist_ok=True)- self.hits = self.misses = self.unreadable = 0+ self.hits = self.misses = self.unreadable = self.not_kept = 0def key(self, interpolate: Interpolate, a: Image.Image, b: Image.Image, t: float) -> str:+ for f in (a, b):+ if f.mode != "RGBA":+ raise ValueError(f"rife: a frame between is kept for RGBA frames only, not {f.mode}: a palette or a "+ "transparent colour is not in an image's bytes, which are what it is kept under")h = hashlib.sha256()for part in (CACHE_FORMAT, interpolate.identity(), a.mode, *map(str, a.size), b.mode, *map(str, b.size), float(t).hex()): # type: ignore[attr-defined]h.update(part.encode() + b"\0")@@ -414,8 +431,10 @@ class Cache:return h.hexdigest()def frame(self, interpolate: Interpolate, a: Image.Image, b: Image.Image, t: float) -> Image.Image:- """The frame at `t` between `a` and `b`: the one kept for exactly these, else `interpolate`'s, kept."""- path = self.directory / f"{self.key(interpolate, a, b, t)}.png"+ """The frame at `t` between `a` and `b`: the one kept for exactly these, else `interpolate`'s — kept where+ its key reads the same once it is made."""+ key = self.key(interpolate, a, b, t)+ path = self.directory / f"{key}.png"if path.exists():kept = _kept(path, a.size)if kept is not None:@@ -426,6 +445,9 @@ class Cache:if made.mode != "RGBA" or made.size != a.size:raise ValueError(f"rife: a frame between is kept as RGBA at its frames' size {a.size}, not {made.mode} at {made.size}")self.misses += 1+ if self.key(interpolate, a, b, t) != key:+ self.not_kept += 1+ return made# saved from a copy of its pixels, so the frame handed on carries nothing of this savepixels = made.tobytes()info = PngImagePlugin.PngInfo()@@ -441,4 +463,5 @@ class Cache:return madedef record(self) -> dict[str, object]:- return {"dir": str(self.directory), "hits": self.hits, "misses": self.misses, "unreadable": self.unreadable}+ return {"dir": str(self.directory), "hits": self.hits, "misses": self.misses, "unreadable": self.unreadable,+ "not_kept": self.not_kept}