Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

fix(size-hold): a cut by hand is held only where it shows the change itself, a cycle of its length apart (gait_fallback.at_cut: each frame of the cut against the frame the caller's length later, or earlier where it runs to the clip's end, at least AT_CUT_SHARE of what the clip reads over one cycle, the same way; its feet likewise) — read over the whole clip, frames that are not the walk (a run's standing start, another motion, a long run-up: -6.9 % on the stop tests' runner) or a clip too short to show its cycle twice inside the lags tried (+2.5 % on the ghost tests' 26-frame walk) read a change the cut does not make (0 at the cut), and the full suite's ghost-given-way and stop cases failed on the frames so scaled; such a cut is cut as filmed, size_hold.at_cut and why say so; walks that do change show 0.94 to 1.07 of it, every synthetic cut as before

Soohong Kim43ba23a0f6eb
diff --git a/sprite_gen/video/gait_fallback.py b/sprite_gen/video/gait_fallback.py
index dad81ec..ecd036f 100644
--- a/sprite_gen/video/gait_fallback.py
+++ b/sprite_gen/video/gait_fallback.py
@@ -69,6 +69,10 @@ SIZE_HOLD_MIN = 0.01
# Under it the feet's row is the box's rounding; a walk away from the camera whose ground recedes up the frame
# climbs by several percent of the body.
FOOT_HOLD_MIN = 0.01
+# The share of the change the clip reads over one of a fixed cut's cycles that the cut must show itself, the same way,
+# to be held (`at_cut`): a walk that grows or shrinks shows the whole change at the cut (0.94 to 1.07 of it on the
+# tests' walkers), frames that are not the walk read into the clip's change show none of it.
+AT_CUT_SHARE = 0.5
# How far off the one-cycle-on model a frame is when it is not yet walking at the clip's size: a first frame
# whose height and size are both this far off, the same way, opens a lead-in, and the frames whose height
# stays this far off, that way, are in it (`lead_in`).
@@ -227,6 +231,42 @@ def foot_drift(frames: list[Image.Image], measured: dict) -> dict:
"lift": -rate * np.arange(n, dtype=np.float64) if held else None}
+def at_cut(frames: list[Image.Image], measured: dict, feet: dict, *, start: int, length: int) -> dict:
+ """Whether a cut the caller names (`--cycle fixed`) changes size, and moves its feet, one cycle on as the clip reads
+ them (`cycle_drift`, `foot_drift`), read on the cut itself at the cycle the caller named: each frame of the cut
+ against the frame `length` later, or `length` earlier where the cut runs to the clip's end.
+
+ The clip's reading is over every frame, so frames that are not the walk the cut is (a standing start, another
+ motion before the walk, a long run-up), or a clip too short to show the walk's cycle twice inside the lags the
+ reading may try, can read a change the walk does not make; the cut's own frames a cycle apart show it or do not.
+ `change` is the median of the cut's height ratios (`coverage_heights`) less 1 and `feet_px` the median move of its
+ feet's row over the same pairs, both over one cycle; `expected` and `feet_expected_px` are the clip's reading
+ carried over one cycle. `size` (`feet`): the cut shows at least AT_CUT_SHARE of the expected change, the same way.
+ No pair (the clip does not show the cut's frames a cycle apart): neither."""
+ n = len(frames)
+ pairs = [(k, k + length) for k in range(start, start + length) if k + length < n]
+ if not pairs:
+ pairs = [(k - length, k) for k in range(start, start + length) if k - length >= 0]
+ expected = (1 + measured["drift"]) ** (length / (n - 1)) - 1
+ feet_expected = feet["change_px"] * length / (n - 1)
+ record = {"pairs": len(pairs), "share": AT_CUT_SHARE, "expected": round(expected, 4),
+ "feet_expected_px": round(feet_expected, 2)}
+ if not pairs:
+ return {**record, "change": None, "feet_px": None, "size": False, "feet": False}
+ heights = coverage_heights(frames)
+ bottoms = subject_boxes(frames)[:, 3]
+ ratios = np.array([heights[b] / heights[a] for a, b in pairs])
+ moves = np.array([bottoms[b] - bottoms[a] for a, b in pairs])
+ change = float(np.median(ratios[np.isfinite(ratios)]) - 1) if np.isfinite(ratios).any() else 0.0
+ moved = float(np.median(moves[np.isfinite(moves)])) if np.isfinite(moves).any() else 0.0
+
+ def shows(seen: float, whole: float) -> bool:
+ return seen * whole > 0 and abs(seen) >= AT_CUT_SHARE * abs(whole)
+
+ return {**record, "change": round(change, 4), "feet_px": round(moved, 2), "size": shows(change, expected),
+ "feet": shows(moved, feet_expected)}
+
+
def lead_in(frames: list[Image.Image], *, min_lag: int, max_lag: int) -> dict:
"""The frames the clip opens on before its subject walks at the clip's size. There are some when the
first frame's height (`coverage_heights`) and size (the square root of its summed coverage) are
diff --git a/sprite_gen/video/loop.py b/sprite_gen/video/loop.py
index d565bb5..afd8dab 100644
--- a/sprite_gen/video/loop.py
+++ b/sprite_gen/video/loop.py
@@ -1641,7 +1641,8 @@ def _blend_wrap(frames: list[Image.Image], pin_file: Path, *, mode: str, anchor:
return wrap_blend_mod.apply(frames, pin, fps=fps, seam_max=seam_max)
-def _size_hold(frames: list[Image.Image], *, lo: int, hi: int) -> tuple[list[Image.Image] | None, dict[str, Any], Any]:
+def _size_hold(frames: list[Image.Image], *, lo: int, hi: int,
+ cut: tuple[int, int] | None = None) -> tuple[list[Image.Image] | None, dict[str, Any], Any]:
"""A walk or run held at its first frame's size, and its feet on the first frame's row, before its cut is chosen
(`--size-hold auto`): the frames held (None: the clip changes size by less than SIZE_HOLD_MIN, read one cycle on,
and is cut as filmed), the report's `size_hold`, and where a box of a frame as filmed lands in its held frame
@@ -1651,7 +1652,9 @@ def _size_hold(frames: list[Image.Image], *, lo: int, hi: int) -> tuple[list[Ima
the first and the loop pops at the wrap; a walk away from the camera climbs the frame as well, and held at its size
with its feet where they were filmed it pops up instead. The change is read one cycle on, so a first pose that
settles into the walk is not taken for a body that shrinks, nor for feet that climb (gait_fallback.cycle_drift,
- foot_drift)."""
+ foot_drift). A cut the caller names (`cut`: start, length) is held only where it shows the change itself, a cycle
+ of its length apart (`gait_fallback.at_cut`), and its feet are moved only where it shows them move: a clip whose
+ frames that are not the walk read a change the cut does not make is cut as filmed (`size_hold.at_cut`, `why`)."""
measured = gait_fallback.cycle_drift(frames, min_lag=lo, max_lag=hi)
hold: dict[str, Any] = {"applied": False, "min": gait_fallback.SIZE_HOLD_MIN,
**{k: measured[k] for k in ("height_first_px", "height_last_px", "drift", "method",
@@ -1659,6 +1662,15 @@ def _size_hold(frames: list[Image.Image], *, lo: int, hi: int) -> tuple[list[Ima
if abs(measured["drift"]) < gait_fallback.SIZE_HOLD_MIN:
return None, hold, None
feet = gait_fallback.foot_drift(frames, measured)
+ if cut is not None:
+ seen = hold["at_cut"] = gait_fallback.at_cut(frames, measured, feet, start=cut[0], length=cut[1])
+ if not seen["size"]:
+ hold["why"] = ("the cut does not change size a cycle of its length apart as the clip reads it (frames that are "
+ "not the walk, or a clip too short to show its cycle twice): cut as filmed")
+ return None, hold, None
+ if feet["held"] and not seen["feet"]:
+ feet = {**feet, "held": False, "lift": None,
+ "why": "the cut's feet do not move a cycle of its length apart as the clip reads them"}
lift = feet.pop("lift")
pad = gait_fallback.undo_padding(frames, measured, lift)
held = gait_fallback.undo_scale(frames, measured, pad=pad, lift=lift)
@@ -1916,7 +1928,7 @@ def _cut_loop(
# A walk or run cut the caller names is held as a search's cut is (`_size_hold`), read over the whole clip,
# and cut on the frames held: the cut's distances, its count and the seam the gate reads are the frames
# that play. The clip is then the clip held (a fixed cut leaves no lead-in out).
- held, report_base["size_hold"], box_of = _size_hold(gait_frames, lo=lo, hi=hi)
+ held, report_base["size_hold"], box_of = _size_hold(gait_frames, lo=lo, hi=hi, cut=(start, length))
if held is not None:
gait_frames = held
files = gait_fallback.write_frames(held, [f.name for f in files], scratch.new())
diff --git a/tests/video/test_gait_fallback.py b/tests/video/test_gait_fallback.py
index f2f8b89..8092122 100644
--- a/tests/video/test_gait_fallback.py
+++ b/tests/video/test_gait_fallback.py
@@ -7,6 +7,8 @@ import numpy as np
import pytest
from PIL import Image, ImageDraw
from sprite_gen.video import gait_fallback, loop
+from tests.video import test_ghost_given_way as ghost_clips
+from tests.video import test_stop as stop_clips
FOOT = (140, 170)
@@ -383,6 +385,8 @@ def test_a_walk_cut_again_by_hand_is_held_at_its_size_and_its_feet(tmp_path, cli
hold = report['size_hold']
assert hold['applied'] is True and abs(hold['drift']) >= gait_fallback.SIZE_HOLD_MIN
assert hold['feet']['held'] is (clip == 'walking away')
+ # The cut shows the change itself, a cycle of its length apart.
+ assert hold['at_cut']['pairs'] == 24 and hold['at_cut']['size'] is True and hold['at_cut']['feet'] is (clip == 'walking away')
assert report['resampled_seam_ratio'] <= loop.SEAM_RATIO_MAX
assert all(wrap <= inner for wrap, inner in edges.values()), edges
assert not list(output.glob(f'.{loop.FALLBACK_FRAMES}*'))
@@ -420,6 +424,35 @@ def test_a_walk_that_keeps_its_size_is_cut_as_filmed_either_way(tmp_path, cut):
assert (outputs[0]/path).read_bytes() == (outputs[1]/path).read_bytes(), path
[email protected]('clip', ['another motion first', 'a short clip'])
+def test_a_cut_by_hand_that_keeps_its_size_is_not_held_for_what_the_clip_reads(tmp_path, clip):
+ """Read over the whole clip, frames that are not the walk the cut is can read a change of size the walk never makes:
+ a run filmed from a standing still that raises and waves its arm and crouches before it sets off
+ (tests/video/test_stop.py) reads 6.9 % smaller by the end, and a walk of 26 frames cut 20 long, too short for the
+ reading to try a lag of its cycle, reads 2.5 % larger. The cut shows no change a cycle of its length apart
+ (`size_hold.at_cut`), so it is cut as filmed: the same bytes as with `--size-hold off`, and the report says why."""
+ if clip == 'another motion first':
+ keyed, run_from = stop_clips.clip(tmp_path, other=8)
+ start, length, state, pairs = run_from+stop_clips.PERIOD, stop_clips.PERIOD, 'run', stop_clips.PERIOD
+ else:
+ keyed = ghost_clips._keyed(tmp_path, 'short', ())
+ start, length, state, pairs = 0, ghost_clips.L, 'walk', 6
+ frames = [Image.open(p).convert('RGBA') for p in sorted(keyed.glob('*.png'))]
+ how = ('--cycle', 'fixed', '--start', str(start), '--length', str(length), '--anchor', 'none', '--seam-max', '1000')
+ outputs = {}
+ for mode in ('auto', 'off'):
+ (tmp_path/mode).mkdir()
+ code, _, outputs[mode] = run(tmp_path/mode, frames, '--size-hold', mode, cut=how, state=state)
+ assert code == 0, code
+ hold = json.loads((tmp_path/'auto'/'loop.json').read_text())['size_hold']
+ assert hold['applied'] is False and abs(hold['drift']) >= gait_fallback.SIZE_HOLD_MIN
+ seen = hold['at_cut']
+ assert seen['pairs'] == pairs and seen['change'] == 0 and seen['size'] is False and 'why' in hold
+ for path in ('loop.strip.png', 'cycle/frame-000.png'):
+ assert (outputs['auto']/path).read_bytes() == (outputs['off']/path).read_bytes(), path
+
+
@pytest.mark.slow
def test_an_idle_cut_by_hand_is_not_held(tmp_path):
# The hold is a walk's or a run's: an idle that changes size is cut as filmed, and its report has no `size_hold`.