Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(video-loop): a frame between refused as held is made again only on the side away from the picture it showed, and not again where it showed both
Soohong Kim0963f6222bc0
diff --git a/sprite_gen/video/held_fill.py b/sprite_gen/video/held_fill.pyindex 1275880..2a71544 100644--- a/sprite_gen/video/held_fill.py+++ b/sprite_gen/video/held_fill.py@@ -33,8 +33,9 @@ is judged as the frames the alignment makes are (`align.measure_made`, `faults`)sides of it (`still_kept`: a foot planted, a hand on a ledge), which must stand where it stood, and for a picture of itsown (`same_picture`, `held`): one that shows the picture of either frame it is made between, or of the cell before it,is no frame between but a held cell again. A frame refused is made again at another fraction of RETIME_T within half a-cell of its own time, else the run is kept as filmed and says why. A run is made whole or kept whole: half made it would-step back.+cell of its own time, else the run is kept as filmed and says why. One refused as `held` is made again only on the side+away from the picture it showed, and not again where it showed both: the frames between run from one pose to the next,+so one nearer that picture shows it too. A run is made whole or kept whole: half made it would step back.`upsample` gives a cut at a rate above its own (`--out-fps`) its frames between, at their own times, judged as thefill's are, never a frame shown again; a cell where both frames beside its time show one picture (a hold kept) is that@@ -164,16 +165,17 @@ def judge(frame: Image.Image, a: Image.Image, b: Image.Image, t: float, *, readsbefore: Image.Image | None = None) -> tuple[dict[str, Any], list[str]]:"""A frame made between `a` and `b` at `t`: the alignment's measure and faults (`align.measure_made`, `faults`),`contact` where a part standing still on both sides does not stand where it stood (`still_kept`), and `held` where- it shows the same picture as `a`, `b` or the cell delivered `before` it (`same_picture`): no frame between, but a- held cell again."""+ it shows the same picture as `a`, `b` or the cell delivered `before` it (`same_picture`; which of them, `shows`): no+ frame between, but a held cell again."""from sprite_gen.video.align import measure_made # align imports loop, which imports this modulemeasure = measure_made(frame, a, b, t, reads=reads)contact = still_kept(frame, a, b)- held = same_picture(frame, (a, b) if before is None else (a, b, before))+ shows = [name for name, other in (("a", a), ("b", b), ("before", before))+ if other is not None and same_picture(frame, (other,))]wrong = (faults(measure) + (["contact"] if contact["reads"] and contact["kept"] < STILL_KEPT_MIN else [])- + (["held"] if held else []))- return {**measure, "contact": contact}, wrong+ + (["held"] if shows else []))+ return {**measure, "contact": contact, "shows": shows}, wrongdef _made(interpolate: rife_mod.Interpolate, a: Image.Image, b: Image.Image, t: float, window: tuple[float, float], *,@@ -181,18 +183,28 @@ def _made(interpolate: rife_mod.Interpolate, a: Image.Image, b: Image.Image, t:"""The frame between `a` and `b` at `t`, or at the fraction of RETIME_T nearest it inside `window` that is madewith no fault (`judge`, `before` the cell delivered before it, None where that is `a`) and, made only off its time,with its ghost band under RETIME_WINDOW_MAX in every window (align's bar for a frame made again at another- fraction); None and what each try was refused for, where none is."""+ fraction); None and what each try was refused for, where none is. The frames between run from `a` to `b`, so one+ that shows `a` or the cell before it again (`held`, `shows`) is made again only later, one that shows `b` only+ earlier, and one that shows both not again: a fraction on the side of a picture it showed would show it too."""from sprite_gen.video.align import RETIME_T, RETIME_WINDOW_MAX+ lo, hi = windowtries = []for at in [t] + sorted((r for r in RETIME_T if window[0] < r < window[1] and abs(r - t) > 1e-3), key=lambda r: (abs(r - t), r)):+ if not lo < at < hi:+ continueframe = interpolate(a, b, at)measure, wrong = judge(frame, a, b, at, reads=reads, before=before)seen = at != t and (measure.get("ghost_window") or 0.0) >= RETIME_WINDOW_MAX- tries.append({"t": round(at, 4), "faults": wrong, **({"window_over": RETIME_WINDOW_MAX} if seen and not wrong else {})})+ tries.append({"t": round(at, 4), "faults": wrong, **({"shows": measure["shows"]} if measure["shows"] else {}),+ **({"window_over": RETIME_WINDOW_MAX} if seen and not wrong else {})})if not wrong and not seen:return frame, {"t": round(at, 4), **({"from_t": round(t, 4)} if at != t else {}),"contact": measure["contact"], "look": looks(measure), "tries": tries}+ if "b" in measure["shows"]:+ hi = at+ if "a" in measure["shows"] or "before" in measure["shows"]:+ lo = atreturn None, {"t": round(t, 4), "tries": tries}diff --git a/tests/video/test_held_fill.py b/tests/video/test_held_fill.pyindex b3703d3..d9b29f6 100644--- a/tests/video/test_held_fill.py+++ b/tests/video/test_held_fill.py@@ -158,8 +158,9 @@ def test_a_stalled_walk_has_its_held_cells_made_between_the_poses_beside_them(tm@pytest.mark.parametrize("given", ["the frame before", "the frame after"])def test_an_interpolator_that_gives_the_pose_back_leaves_the_stalled_walk_as_filmed_and_says_so(given, tmp_path, capsys):- # No frame between at all: each frame it gives is a pose beside it, a held cell itself. Tried again at other- # fractions, never taken: the cut is delivered as filmed, its record and its warnings say both runs are still held.+ # No frame between at all: each frame it gives is a pose beside it, a held cell itself. Tried again only on the+ # side away from that pose (later for the frame before, earlier for the frame after) within half a cell, never+ # taken: the cut is delivered as filmed, its record and its warnings say both runs are still held.calls: list[float] = []def stuck(a, b, t):@@ -172,8 +173,11 @@ def test_an_interpolator_that_gives_the_pose_back_leaves_the_stalled_walk_as_filassert record["made"] == [] and "interpolator" not in recordruns = record["before"]["held_runs"]assert [(r["start"], r["length"], r["filled"]) for r in runs] == [(5, 2, False), (14, 3, False)]- assert all("held" in r["faults"] and len(r["tries"][0]["tries"]) > 1 for r in runs)- assert len(calls) == sum(len(r["tries"][0]["tries"]) for r in runs)+ assert all("held" in r["faults"] for r in runs)+ assert [[t["t"] for t in r["tries"][0]["tries"]] for r in runs] == (+ [[0.5, 0.55, 0.6, 0.65, 0.7], [0.3333, 0.35, 0.4, 0.45]] if given == "the frame before"+ else [[0.5, 0.45, 0.4, 0.35, 0.3], [0.3333, 0.3, 0.25, 0.2]])+ assert len(calls) == sum(len(r["tries"][0]["tries"]) for r in runs) == 9assert (record["cells"], record["poses"], record["held_runs"]) == (N + 3, N, [{"start": 5, "length": 2}, {"start": 14, "length": 3}])assert _pictures(Path(rep["out_dir"]), "m") == STALLED_OFF@@ -263,12 +267,13 @@ def test_a_once_cut_and_a_one_shot_have_their_held_cells_made_too(tmp_path):record = rep["held_fill"]start = rep["cycle"]["start"]assert [(r["start"], r["filled"]) for r in record["before"]["held_runs"] if r["filled"]] == [(20 - start, True)]- # Half way, and a twentieth either side, the body has sunk less than a pixel: each such frame shows frame 20 again- # and is refused (`held`). The cell is made at the nearest fraction where the body has moved.- assert between.calls == [(20.0, 21.0, t) for t in (0.5, 0.45, 0.55, 0.4, 0.6)]+ # Half way, and a twentieth on, the body has sunk less than a pixel: each such frame shows frame 20 again and is+ # refused (`held`). A frame nearer frame 20 would show it too, so the cell is made again only later, at the nearest+ # fraction where the body has moved.+ assert between.calls == [(20.0, 21.0, t) for t in (0.5, 0.55, 0.6)]made = next(r for r in record["frames"] if r["cell"] == 21 - start)assert (made["t"], made["from_t"]) == (0.6, 0.5)- assert [t["faults"] for t in made["tries"]] == [["held"]] * 4 + [[]]+ assert [(t["faults"], t.get("shows")) for t in made["tries"]] == [(["held"], ["a"])] * 2 + [([], None)]assert all(r["start"] != 20 - start for r in record["held_runs"]) # read again, the cell made is a pose of its ownhopped = shown_again(hop(), {18: 2})rep = _run(tmp_path, hopped, "hop", interpolate=lambda a, b, t: Image.blend(a, b, t), state="jump", cycle_mode="one-shot")@@ -335,11 +340,37 @@ def test_a_frame_between_that_shows_a_pose_beside_it_again_is_not_taken(given):assert record["made"] == [] and [o.tobytes() for o in out] == [f.tobytes() for f in frames]runs = record["before"]["held_runs"]assert all(not r["filled"] and r["faults"] == ["held"] for r in runs)- # Each run's first cell tried at its own time, then at the other fractions within half a cell of it.- assert all(len(r["tries"][0]["tries"]) > 1 and all(t["faults"] == ["held"] for t in r["tries"][0]["tries"]) for r in runs)+ # Each run's first cell tried at its own time, then at the other fractions within half a cell of it on the side+ # away from the pose it showed again: later for the frame before, earlier for the frame after.+ shown, later = ("a", True) if given == "the frame before" else ("b", False)+ for r in runs:+ tries = r["tries"][0]["tries"]+ assert len(tries) > 1 and all(t["faults"] == ["held"] and t["shows"] == [shown] for t in tries)+ assert [t["t"] for t in tries] == sorted((t["t"] for t in tries), reverse=not later)assert (record["poses"], record["held_runs"]) == (N, [{"start": 5, "length": 2}, {"start": 14, "length": 3}])+def _lit(blue: int) -> Image.Image:+ """`planter(10)` with its body `blue` in the blue channel: one pose, lit differently."""+ a = np.asarray(planter(10)).copy()+ a[30:110, 50:70] = (90, 90, blue, 255)+ return Image.fromarray(a, "RGBA")+++def test_a_frame_between_that_shows_both_poses_beside_it_is_not_made_again():+ # The pose after the held one lit 40 levels brighter (over PIXEL_SAME): any frame between them shows one of the+ # two, and half way it shows both again. No fraction can show a picture of its own, so it is made once.+ a, b = _lit(200), _lit(240)+ frames = [planter(0), a, a, b, planter(20)]+ calls: list[float] = []+ out, record = held_fill.fill(frames, lambda x, y, t: calls.append(t) or Image.blend(x, y, t), ring=False)+ run = record["before"]["held_runs"][0]+ assert (run["start"], run["length"], run["filled"], run["faults"]) == (1, 2, False, ["held"])+ assert calls == [0.5]+ assert run["tries"][0]["tries"] == [{"t": 0.5, "faults": ["held"], "shows": ["a", "b"]}]+ assert record["made"] == [] and [o.tobytes() for o in out] == [f.tobytes() for f in frames]++def test_a_run_whose_frames_between_show_one_picture_twice_is_kept_as_filmed():# An interpolator that draws the time half way whatever it is asked: a run of two is made, a run of three would# show its one frame between on both its cells.@@ -349,6 +380,8 @@ def test_a_run_whose_frames_between_show_one_picture_twice_is_kept_as_filmed():runs = record["before"]["held_runs"]assert [(r["start"], r["filled"]) for r in runs] == [(5, True), (14, False)]assert runs[1]["faults"] == ["held"] and [row["cell"] for row in runs[1]["tries"]] == [15, 16]+ # The second shows the first again, the cell before it: made again only later, within half a cell.+ assert [(t["t"], t["shows"]) for t in runs[1]["tries"][1]["tries"]] == [(t, ["before"]) for t in (0.6667, 0.7, 0.75, 0.8)]assert record["made"] == [6] and out[6].tobytes() == walk_at(5.5).tobytes()assert [o.tobytes() for o in out[14:17]] == [f.tobytes() for f in frames[14:17]]assert record["held_runs"] == [{"start": 14, "length": 3}]@@ -441,6 +474,8 @@ def test_a_cell_at_a_higher_rate_whose_frame_between_shows_a_frame_again_is_nameout, record = held_fill.upsample(frames, lambda a, b, t: a, ring=False, fps=24.0, out_fps=60.0)assert record["made"] == 0 and record["shown_again"] == [1, 2, 3, 4, 6, 7, 8, 9]assert all(t["faults"] == ["held"] for m in record["frames"] for t in m["tries"])+ # Each tried again only later than its own time: a frame nearer the one it showed would show it too.+ assert all(t["t"] >= m["t"] for m in record["frames"] for t in m["tries"])assert [o.tobytes() for o in out] == [frames[round(k * 0.4)].tobytes() for k in range(11)]@@ -451,6 +486,8 @@ def test_two_cells_at_a_higher_rate_between_one_pair_are_not_one_picture():between = Between(walk_at, range(5))out, record = held_fill.upsample(frames, lambda a, b, t: between(a, b, 0.5), ring=False, fps=24.0, out_fps=60.0)assert record["shown_again"] == [2, 4, 7, 9] and record["made"] == 4+ # The second shows the first again, the cell before it: made again only later, at 0.85, the last fraction there.+ assert [[t["t"] for t in m["tries"]] for m in record["frames"] if m["cell"] == 2] == [[0.8, 0.85]]assert out[1].tobytes() == walk_at(0.5).tobytes() and out[2].tobytes() == frames[1].tobytes()assert out[3].tobytes() == walk_at(1.5).tobytes() and out[4].tobytes() == frames[2].tobytes()