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(chroma): a green screen leaning toward cyan or yellow is the key on the border — the dark bar holds the darker unkeyed channel, and the lean stays with the background that leans

Soohong Kim63563bf23144
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3e904c9..593e09b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
All notable public changes to `sprite-gen` are recorded here. Versions track the `version:` field in `SKILL.md` and `pyproject.toml`.
+## Unreleased
+
+- **Fix: Green screens that lean toward cyan or yellow are now recognised as border keys.** A green screen that leans toward cyan or yellow was not recognised as the key on the border. Examples: the studio chroma-key green `#00B140`, (0, 177, 64); (0, 200, 90), which leans toward cyan; (90, 210, 0), which leans toward yellow. Each sits more than 96 from pure green. With `--key green` the whole background stayed opaque, and `video-canvas` refused `--key green` on such a still; but with `--key auto` the old rule did not take the green route at all: `cutout` and `video-frames` routed the frame to the white (position-based) matte, and `video-canvas` treated the still as a non-key base and did not normalize it. `video-frames --spill auto` did not always choose `full`: when the reference still had the same leaning green, `auto` read the surviving background as key material and chose `small`; when it chose `full` (a reference whose own background the old rule did recognise), the clip's surviving background was despilled to a blue-grey instead of being erased. The cause was that the border rule `extract.is_border_key_candidate` held every unkeyed channel under 64. That bar was set between magentas, whose one unkeyed channel (green) tells a painted magenta from hot pink. Green has two unkeyed channels, and a green screen leans on one of them. For the fix, the darkest unkeyed channel keeps the bar (under 64, and at most 35 % of the brightest keyed channel). The brightest one only has to stay under half of the brightest keyed channel (`extract._BORDER_KEY_MAX_LEAN_RATIO`, 0.5), that is a hue nearer green than yellow or cyan (between 90 and 150 degrees). The lean is evidence about the background that leans, so the painted key's erase ball, the spill judgment's subject window and the `video-frames` edge-contact split take the lean only when the painted colour leans itself (`extract._painted_key_leans`); the border detector, `cutout --key auto` and `video-canvas --key` always apply it (there the question is what the painted colour can be). The magenta key is exactly as before (its one unkeyed channel is both the darkest and the brightest), so hot pink (250, 77, 150) and purple (213, 112, 246) are still refused. For green, nothing that was a candidate is dropped. Ivory, cyan, teal, sea green, a grey-green with both unkeyed channels lit and a green as near cyan as green are still refused. The interior rule `is_key_family` is unchanged. So on every background the rule took before (pure green, darker family greens such as (8, 162, 24), the magentas), as long as the background, not a leaning-green part of the subject, fills most of the border samples, every keyed result is byte for byte the same as before; a leaning-green patch buried in the subject on such a background is kept. [chroma-alpha](docs/chroma-alpha.md), the 2026-10-21 paragraph.
+- **Tests** - `tests/frames/test_chroma_key_relative.py` (the border table with the three leaning greens accepted and a grey-green and a cyan-midpoint green refused; the detector still refuses hot pink, purple, ivory, cyan, (30, 60, 30), teal, sea green and those two; over the whole RGB cube the lean adds nothing for magenta, drops nothing for either key, and adds for green only hues between 90 and 150 degrees; without the lean the candidate field is the old rule colour for colour over the whole RGB cube; the engine and `cutout --key green` key the leaning greens out with the subject byte-identical; `cutout --key auto` routes a leaning green to `extract:green`; a leaning-green patch ((0, 160, 75) or (10, 120, 50)) buried in the subject is kept byte for byte on (8, 162, 24) and keyed with the background on (0, 200, 90); the edge-contact split reads a leaning piece as residual when the frame was keyed from a leaning background and as the subject's when it was keyed from (8, 162, 24)), `tests/video/test_canvas_key_normalize.py` (`video-canvas` repaints a leaning-green still to the exact key and accepts `--key green` on it), and `tests/video/test_video_spill.py` (on a leaning green, `--spill auto` reads the reference as `full` with no key material, and the frame keys with no opaque background pixel and no edge contact, the subject byte-identical, with `--decontam off` and `palette`).
+
## v2.45.2 - The new `video-loop --overlap-wrap off` turns the overlapped wrap off on its own; the motion anchor's fine search is faster and gives the same values
**Upgrading from 2.45.1: By default a run delivers the same bytes as 2.45.1, sooner, and only `--overlap-wrap off` changes what is delivered.** With the default `--overlap-wrap on`, the loop files, the frames and the report are the same bytes as 2.45.1 (the report's `producer` field names the new version), and `video-loop --anchor motion-auto` finishes sooner. With `--overlap-wrap off`, the overlapped wrap is not tried (the report's `wrap_overlap.skipped` reads `turned off (--overlap-wrap off)`, no RIFE calls), a `warning:` line is printed, and the seam gate refuses the cut as filmed, so the run fails on its seam instead of spending minutes of CPU on RIFE. No restoration schemas change: `source-restoration-v5`, repair report `schema_version` 4, comparison `schema_version` 6.
diff --git a/docs/chroma-alpha.md b/docs/chroma-alpha.md
index bd63628..2c18c00 100644
--- a/docs/chroma-alpha.md
+++ b/docs/chroma-alpha.md
@@ -46,7 +46,9 @@ The close-up crops below show the edge detail behind the full-body comparisons.
- Blue subjects → avoid cyan/blue keys; magenta or green.
- Both pink *and* green in one subject → pick the key farther from the larger/more important material, verify both survive after extraction, and prefer `--chroma-key auto` (distance-scored).
-**The border and the interior answer different questions** (2026-09-12). `detect_background_key_rgb` classifies the border samples with `is_border_key_candidate` — keyed channels lit, unkeyed channels dark (< 64) and under 35 % of the brightest keyed channel, no balance between the keyed channels — because a flat border is already background evidence: Grok paints `#FF00FF` as (216, 46, 147), blue/red 0.68, which the interior rule `is_key_family` (balance ≥ 0.8) rejected, so the detector fell back to the declared key and the whole background survived. The interior rule keeps the balance so hot pink (250, 77, 150) and purple (213, 112, 246) inside the subject stay byte-identical; they fail the border rule too, on the lit green channel. The painted key's erase ball only reaches pixels that carry the signature or are 8-connected to them through the ball (the antialiased rim) — an isolated look-alike patch inside the subject is not erased. `cutout --key auto`, `video-canvas --key` and the `video-frames` residual split use the same border function.
+**The border and the interior answer different questions** (2026-09-12). `detect_background_key_rgb` classifies the border samples with `is_border_key_candidate` (keyed channels lit, the darkest unkeyed channel dark (< 64) and under 35 % of the brightest keyed channel, the brightest unkeyed channel under half of the brightest keyed channel, with the half bound explained in the 2026-10-21 paragraph that follows, no balance between the keyed channels) because a flat border is already background evidence: Grok paints `#FF00FF` as (216, 46, 147), blue/red 0.68, which the interior rule `is_key_family` (balance ≥ 0.8) rejected, so the detector fell back to the declared key and the whole background survived. The interior rule keeps the balance so hot pink (250, 77, 150) and purple (213, 112, 246) inside the subject stay byte-identical; they fail the border rule too, on the lit green channel. The painted key's erase ball only reaches pixels that carry the signature or are 8-connected to them through the ball (the antialiased rim) — an isolated look-alike patch inside the subject is not erased. `cutout --key auto`, `video-canvas --key` and the `video-frames` residual split use the same border function.
+
+**The border rule accepts green leaning toward cyan or yellow** (2026-10-21). Before this change the border rule was: everything the interior rule `is_key_family` admits, or a colour with every keyed channel lit (>= 64), every unkeyed channel dark (< 64) and every unkeyed channel at most 35 % of the brightest keyed channel. The dark bar of 64 was set between magentas: the magenta key has one unkeyed channel (green), and that channel is what tells a painted magenta key from hot pink (250, 77, 150) and purple (213, 112, 246), whose green is lit (77, 112). The green key has two unkeyed channels (red and blue). A green screen leans on one of them. The studio chroma-key green `#00B140` is (0, 177, 64): its blue sits on the bar, not under it. (0, 200, 90) leans further toward cyan. Neither is interior family either (blue is over 35 % of green). Both sit more than 96 from pure green (100.9 and 105.5), so with the detector falling back to the declared key, the whole background survived the hard cut. Under `video-frames --spill auto` choosing `full`, the surviving background was then treated as spill on the subject and despilled to a blue-grey instead of being erased. The rule now: for the unkeyed channels, the darkest keeps the old bars (< 64, and at most 35 % of the brightest keyed channel), and the brightest only has to stay under half of the brightest keyed channel (`_BORDER_KEY_MAX_LEAN_RATIO`, 0.5). Under half means the hue sits nearer the key than the neighbouring secondary colour: green is at 120 degrees, yellow at 60 and cyan at 180, so the midpoints are 90 and 150. The lean is accepted in both directions: toward cyan (blue lit, e.g. (0, 200, 90)) and toward yellow (red lit, e.g. (90, 210, 0)). A key with one unkeyed channel (magenta) is unchanged: its darkest and brightest unkeyed channel are the same channel, so it still has to be under 64, and hot pink and purple are still refused on their lit green. Over the whole RGB cube, the magenta candidates are exactly what they were; for green nothing is dropped, and every added colour has a hue strictly between 90 and 150 degrees. Still refused for green: ivory (248, 247, 242), cyan (0, 255, 255), teal (0, 128, 128), sea green (46, 139, 87), a grey-green with both unkeyed channels lit (70, 200, 90), and a green exactly at the cyan midpoint (0, 200, 100), where blue is half of green. The painted key's erase ball is unchanged in shape: inside the ball it erases the pixels that carry the signature and whatever is 8-connected to them through the ball (the antialiased rim). A rim made of brown blended into a leaning green is cut with the background; the same blend buried inside the subject stays. The lean is evidence about the background that leans. So inside the frame, the lean widens only the erase ball of a painted colour that leans itself (`extract._painted_key_leans`: the painted colour is a border candidate only through the lean). For a painted colour that does not lean, the ball's seeds and the edge-contact split use the signature with every unkeyed channel dark, which is the border rule as it was before (`_border_key_candidate_field(..., lean=False)` is that rule, colour for colour, over the whole RGB cube). Consequence: on every background the rule took before (pure green, a darker family green such as (8, 162, 24), pure magenta, Grok's magenta), every keyed result is byte for byte what it was as long as the background, not a leaning-green part of the subject, is what fills most of those border samples; a subject whose leaning green outnumbers the background on the corners and the border can now be taken as the painted colour. For example a leaning-green patch such as (0, 160, 75) or (10, 120, 50) buried in the subject on (8, 162, 24) is kept. On a background that leans, a patch inside the subject that leans with it and sits within 96 of it is keyed, as any signature colour inside the painted ball is. On a leaning background, subject colours within 96 of the painted colour and joined to the background are keyed with it (for example sea green (46, 139, 87) or teal (0, 128, 128) on (0, 200, 90)), the same ball the engine uses for every painted key; on the already supported (8, 162, 24), leaf (60, 140, 60) and sea green joined to the background are keyed the same way. Colours off the key, such as brown, skin, highlight, hot pink, purple, ivory, cyan and turquoise, are not keyed on any of these backgrounds. Where the painted colour is found and where the frame is routed (`detect_background_key_rgb`, `cutout --key auto`, `video-canvas --key`) the lean always applies: there the question is what the painted colour can be. One function still classifies the border everywhere: `detect_background_key_rgb`, `cutout --key auto`, `video-canvas --key` and the `video-frames` edge-contact split, as well as the painted ball in the hard cut and the subject window the spill judgment keys, but the ball, the subject window and the `video-frames` edge-contact split take the lean only from a painted colour that leans (the edge-contact split reads the frame's painted colour).
## `--chroma-key auto`
diff --git a/docs/video-pipeline.md b/docs/video-pipeline.md
index 5fede81..38776ca 100644
--- a/docs/video-pipeline.md
+++ b/docs/video-pipeline.md
@@ -576,10 +576,13 @@ pink (250, 77, 150) and purple (213, 112, 246) alive *inside* the subject. Grok
the key" for a colour that filled the whole border, the detector fell back to the declared
key, and at ~120 from pure magenta the entire background survived. A flat border is
already evidence of *background*, so the border rule `extract.is_border_key_candidate`
-drops the balance and keeps the hue signature: every keyed channel lit (≥ 64), every
-unkeyed channel dark (< 64) and under 35 % of the brightest keyed channel. It is a
-superset of the interior rule (family ∪ signature), so nothing the old detector found is
-lost, and hot pink / purple fail it on a different axis (their green channel is lit). One
+drops the balance and keeps the hue signature: every keyed channel lit (>= 64), the
+darkest unkeyed channel dark (< 64) and under 35 % of the brightest keyed channel, and the
+brightest unkeyed channel under half of it, so a green screen that leans toward cyan or
+yellow, such as the studio green (0, 177, 64), is the key while a colour as near cyan or
+yellow as green is not. It is a superset of the interior rule (family union signature), so
+nothing the old detector found is lost, and hot pink and purple fail on their lit green
+channel, which is magenta's only unkeyed channel. One
function classifies the border everywhere — `detect_background_key_rgb`, `cutout --key
auto`, `video-canvas --key` and the edge-contact split below. The interior rule is
untouched. And because the painted colour's authority is border evidence, its erase ball
diff --git a/sprite_gen/frames/extract.py b/sprite_gen/frames/extract.py
index f0aaa99..91072d6 100644
--- a/sprite_gen/frames/extract.py
+++ b/sprite_gen/frames/extract.py
@@ -232,7 +232,21 @@ _KEY_FAMILY_KEYED_BALANCE = 0.8 # for two-channel keys (magenta) the dimmer key
# painted keys' never is (46..59). So the border rule keeps the hue signature — keyed
# channels lit, unkeyed channels dark and saturated against the *brighter* keyed
# channel — and drops the balance between the keyed channels.
+# Leaning green (2026-10-21): the dark bar was set between magentas, whose one unkeyed
+# channel is what tells a painted key from hot pink. Green has two, and a green screen
+# leans on one of them — the studio chroma-key green #00B140 is (0, 177, 64), blue on the
+# bar — so holding every unkeyed channel under the bar refused a whole painted background
+# that leans a little further. For a key with two unkeyed channels the darker one keeps
+# the bar, and the one the colour leans on only has to stay under half the brightest
+# keyed channel: the hue then sits nearer the key than the neighbouring secondary (green
+# 120 degrees, cyan 180 and yellow 60, the midpoints 150 and 90). A key with one unkeyed
+# channel is unchanged — its darker and leaning channel are the same one, under the bar —
+# so hot pink's lit green still refuses it. The lean is evidence about the background that
+# leans, so inside the frame it widens only the ball of a painted colour that leans itself
+# (`_painted_key_leans`): on a background the rule took before, a leaning-green patch in
+# the subject is not the painted colour's and stays.
_BORDER_KEY_MAX_UNKEYED_RATIO = _KEY_FAMILY_MAX_UNKEYED_RATIO # against the brightest keyed channel, not the dimmest
+_BORDER_KEY_MAX_LEAN_RATIO = 0.5 # the unkeyed channel a key leans on stays under half the brightest keyed channel
_KEY_DETECT_CORNER_DIV = 5 # corner patch = width/5 x height/5 (same sampling as the YCbCr path)
_KEY_DETECT_MIN_FRACTION = 0.12 # family share of the samples needed to trust the detection
_KEY_DETECT_BIN_SHIFT = 3 # RGB histogram bin width 8 — the mode bin, never a mean, picks the colour
@@ -271,22 +285,27 @@ def is_border_key_candidate(color: tuple[int, int, int], chroma_key: tuple[int,
and the `video-frames` edge-contact split. A superset of `is_key_family`:
everything the interior rule admits, plus colours that carry the key's hue
*signature* without the keyed channels balancing — every keyed channel lit
- (>= 64), every unkeyed channel dark (< 64, the key split's own bar) and
- under 35% of the *brightest* keyed channel. So Grok's (216, 46, 147)
- magenta is a candidate; hot pink (250, 77, 150) and purple (213, 112, 246)
- are not (their green channel is lit), nor are ivory and cyan. The interior
- rule stays `is_key_family` — a pixel *inside* the frame has no border evidence.
+ (>= 64), the darkest unkeyed channel dark (< 64, the key split's own bar)
+ and under 35% of the *brightest* keyed channel, and the brightest unkeyed
+ channel — the one a green screen leans on — under half of it. So Grok's
+ (216, 46, 147) magenta is a candidate, and so are greens leaning toward cyan
+ or yellow such as (0, 177, 64) and (0, 200, 90); hot pink (250, 77, 150) and
+ purple (213, 112, 246) are not (their green channel, magenta's only unkeyed
+ one, is lit), nor are ivory and cyan. The interior rule stays
+ `is_key_family` — a pixel *inside* the frame has no border evidence.
"""
keyed_channels, unkeyed_channels = _key_channel_split(chroma_key)
if not keyed_channels:
return False
keyed_min = min(color[index] for index in keyed_channels)
keyed_max = max(color[index] for index in keyed_channels)
+ unkeyed_min = min(color[index] for index in unkeyed_channels)
unkeyed_max = max(color[index] for index in unkeyed_channels)
return is_key_family(color, chroma_key) or (
keyed_min >= _KEY_FAMILY_MIN_KEYED
- and unkeyed_max < _KEY_CHANNEL_DARK
- and unkeyed_max <= keyed_max * _BORDER_KEY_MAX_UNKEYED_RATIO
+ and unkeyed_min < _KEY_CHANNEL_DARK
+ and unkeyed_min <= keyed_max * _BORDER_KEY_MAX_UNKEYED_RATIO
+ and unkeyed_max < keyed_max * _BORDER_KEY_MAX_LEAN_RATIO
)
@@ -302,19 +321,44 @@ def _key_family_field(rgb: np.ndarray, keyed_channels: list[int], unkeyed_channe
)
-def _border_key_candidate_field(rgb: np.ndarray, keyed_channels: list[int], unkeyed_channels: list[int]) -> np.ndarray:
- """Vector form of `is_border_key_candidate` — same inequalities, same constants."""
+def _border_key_candidate_field(rgb: np.ndarray, keyed_channels: list[int], unkeyed_channels: list[int], *,
+ lean: bool = True) -> np.ndarray:
+ """Vector form of `is_border_key_candidate` — same inequalities, same constants.
+
+ `lean=False` holds the brightest unkeyed channel to the darkest one's bars as well (every
+ unkeyed channel dark): the signature as a painted colour that does not lean carries it
+ (`_painted_key_leans`), which is the border rule as it was before the lean.
+ """
keyed_min = rgb[..., keyed_channels].min(axis=-1)
keyed_max = rgb[..., keyed_channels].max(axis=-1)
+ unkeyed_min = rgb[..., unkeyed_channels].min(axis=-1)
unkeyed_max = rgb[..., unkeyed_channels].max(axis=-1)
signature = (
(keyed_min >= _KEY_FAMILY_MIN_KEYED)
- & (unkeyed_max < _KEY_CHANNEL_DARK)
- & (unkeyed_max <= keyed_max * _BORDER_KEY_MAX_UNKEYED_RATIO)
+ & (unkeyed_min < _KEY_CHANNEL_DARK)
+ & (unkeyed_min <= keyed_max * _BORDER_KEY_MAX_UNKEYED_RATIO)
+ & (unkeyed_max < keyed_max * _BORDER_KEY_MAX_LEAN_RATIO)
)
+ if not lean:
+ signature &= (unkeyed_max < _KEY_CHANNEL_DARK) & (unkeyed_max <= keyed_max * _BORDER_KEY_MAX_UNKEYED_RATIO)
return _key_family_field(rgb, keyed_channels, unkeyed_channels) | signature
+def _painted_key_leans(painted_key: tuple[int, int, int], keyed_channels: list[int], unkeyed_channels: list[int]) -> bool:
+ """Whether the painted colour is the key only through the lean: a green screen leaning toward cyan or yellow.
+
+ The lean is border evidence about *that* background. What its ball claims inside the frame
+ (`hard_key_mask`, `subject_window`) and which edge pixels read as leftover background
+ (`video-frames`' edge-contact split) lean as far as the painted colour gives them leave to:
+ on a background that leans, the colours that lean with it; on one that does not, only the
+ signature with every unkeyed channel dark — so a leaning-green patch buried in the subject on
+ a background the rule took before the lean stays as it was.
+ """
+ rgb = np.asarray([painted_key], dtype=np.int32)
+ return bool(_border_key_candidate_field(rgb, keyed_channels, unkeyed_channels)[0]
+ and not _border_key_candidate_field(rgb, keyed_channels, unkeyed_channels, lean=False)[0])
+
+
def _key_detect_sample_mask(height: int, width: int, top: int = 0, bottom: int | None = None) -> np.ndarray:
"""Corner patches (w/5 x h/5) plus the 1-px border — where the background lives.
@@ -440,11 +484,14 @@ def hard_key_mask(source_rgb: np.ndarray, alpha: np.ndarray, chroma_key: tuple[i
# unkeyed channel over the signature bar). An isolated patch inside
# the subject that merely resembles the painted colour is left alone:
# hot pink (250, 77, 150) sits 46 from Grok's (216, 46, 147) magenta
- # and would otherwise be cut out of the subject. The declared key's
- # ball stays what it always was — a colour ball, position-blind.
+ # and would otherwise be cut out of the subject. The signature is the
+ # one the painted colour carries: the leaning greens only when it leans
+ # itself (`_painted_key_leans`). The declared key's ball stays what it
+ # always was — a colour ball, position-blind.
painted_distance = _key_distance_field(source_rgb, painted_key)
in_ball = painted_distance <= threshold
- painted_keyed = in_ball & _border_key_candidate_field(source_rgb, keyed_channels, unkeyed_channels)
+ lean = _painted_key_leans(painted_key, keyed_channels, unkeyed_channels)
+ painted_keyed = in_ball & _border_key_candidate_field(source_rgb, keyed_channels, unkeyed_channels, lean=lean)
painted_keyed |= in_ball & (key_distance <= threshold)
painted_keyed = _grow_into(painted_keyed, in_ball)
key_distance = np.where(painted_keyed, np.minimum(key_distance, painted_distance), key_distance)
@@ -460,7 +507,8 @@ def subject_window(image: Image.Image, chroma_key: tuple[int, int, int], thresho
`hard_key_mask` keys, whatever their alpha, every pixel within `threshold` of the
declared key and, when the painted colour differs, every pixel within `threshold` of
- the painted colour that carries the key's border signature (`is_border_key_candidate`).
+ the painted colour that carries the key's border signature (`is_border_key_candidate`,
+ leaning only when the painted colour leans: `_painted_key_leans`).
Only the other pixels can stay opaque. Returns their bounding box grown by `margin`
and clipped to the image, as (left, top, right, bottom), or None when there are none
(the whole image keys out). Pass the painted colour the engine will use.
@@ -479,6 +527,7 @@ def subject_window(image: Image.Image, chroma_key: tuple[int, int, int], thresho
"""
keyed_channels, unkeyed_channels = _key_channel_split(chroma_key)
ball = painted_key is not None and tuple(painted_key) != tuple(chroma_key) and bool(keyed_channels)
+ lean = ball and _painted_key_leans(painted_key, keyed_channels, unkeyed_channels) # type: ignore[arg-type]
width, height = image.size
rows = np.zeros(height, dtype=bool)
cols = np.zeros(width, dtype=bool)
@@ -487,7 +536,7 @@ def subject_window(image: Image.Image, chroma_key: tuple[int, int, int], thresho
erased = _key_distance_field(rgb, chroma_key) <= threshold
if ball:
erased |= ((_key_distance_field(rgb, painted_key) <= threshold)
- & _border_key_candidate_field(rgb, keyed_channels, unkeyed_channels))
+ & _border_key_candidate_field(rgb, keyed_channels, unkeyed_channels, lean=lean))
rows[top:top + len(rgb)] = ~erased.all(axis=1)
cols |= ~erased.all(axis=0)
if not rows.any():
diff --git a/sprite_gen/video/frames.py b/sprite_gen/video/frames.py
index c3e4e55..e401c7b 100644
--- a/sprite_gen/video/frames.py
+++ b/sprite_gen/video/frames.py
@@ -53,7 +53,7 @@ from PIL import Image
from sprite_gen._deps import np
from sprite_gen.frames.cutout import cutout
from sprite_gen.frames.decontam import palette_from_stats
-from sprite_gen.frames.extract import _border_key_candidate_field, _key_channel_split
+from sprite_gen.frames.extract import _border_key_candidate_field, _key_channel_split, _painted_key_leans
from sprite_gen.frames.extract import _SPILL_FULL_MIN_TINT, DEFAULT_UNMIX_REACH
from sprite_gen.spec.runio import atomic_write_text
@@ -234,13 +234,15 @@ def edge_contact(image: Image.Image) -> dict[str, int]:
return {"top": top, "left": left, "right": right}
-def classify_edge_contact(raw: Image.Image, keyed: Image.Image, chroma_key: tuple[int, int, int] | None) -> dict[str, int]:
+def classify_edge_contact(raw: Image.Image, keyed: Image.Image, chroma_key: tuple[int, int, int] | None,
+ painted_key: tuple[int, int, int] | None = None) -> dict[str, int]:
"""Split the opaque edge-band pixels into `subject` and `residual` (leftover key background).
A contact pixel whose *raw* colour carries the declared key's hue signature
- (`is_border_key_candidate`) is either background the matte failed to erase or the subject's
- own rim: a body reaching the band brings its antialiased edge, the key blended into it, there
- first. It is residual only when the opaque piece of the keyed frame it belongs to — its opaque
+ (`is_border_key_candidate`; the leaning greens only when `painted_key`, the background the
+ frame was keyed from, leans itself: `extract._painted_key_leans`) is either background the
+ matte failed to erase or the subject's own rim: a body reaching the band brings its
+ antialiased edge, the key blended into it, there first. It is residual only when the opaque piece of the keyed frame it belongs to — its opaque
pixels, corners joining them, the pieces `drop_specks` reads — holds no pixel off the key's
signature: background on its own. Joined to one, it is the subject's. Without a chroma key
(matte route) every contact is the subject.
@@ -257,7 +259,9 @@ def classify_edge_contact(raw: Image.Image, keyed: Image.Image, chroma_key: tupl
keyed_channels, unkeyed_channels = _key_channel_split(chroma_key) if chroma_key is not None else ([], [])
if not total or not keyed_channels:
return {"subject": total, "residual": 0}
- keyish = _border_key_candidate_field(np.asarray(raw.convert("RGB"), dtype=np.int32), keyed_channels, unkeyed_channels)
+ lean = painted_key is not None and _painted_key_leans(painted_key, keyed_channels, unkeyed_channels)
+ keyish = _border_key_candidate_field(np.asarray(raw.convert("RGB"), dtype=np.int32), keyed_channels, unkeyed_channels,
+ lean=lean)
suspect = contact & keyish
if not suspect.any():
return {"subject": total, "residual": 0}
@@ -390,7 +394,9 @@ def key_frames(
contact = edge_contact(image)
if any(v > EDGE_MAX_PIXELS for v in contact.values()):
chroma = stats.get("chroma_key")
- contact.update(classify_edge_contact(Image.open(src), image, tuple(chroma) if chroma else None))
+ painted = stats.get("chroma_key_painted")
+ contact.update(classify_edge_contact(Image.open(src), image, tuple(chroma) if chroma else None,
+ tuple(painted) if painted else None))
contact["key_painted"] = stats.get("chroma_key_painted")
contacts.append({"frame": src.name, **contact})
row["edge"] = contact