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: preserve sparse light colors in opaque GIF palettes

Soohong Kimd7ff8dc14612
diff --git a/docs/effects.md b/docs/effects.md
index de5d703..2b1cf1e 100644
--- a/docs/effects.md
+++ b/docs/effects.md
@@ -79,6 +79,6 @@ GIF and WebP play once. The video GIF ends on a fully opaque black frame; the tr
| Component | Reuse Details |
|---|---|
| `effect.on_black` | Black compositing body extracted unchanged from `effect.canvas` so generation canvas and video GIF call one function |
-| `util.gif_utils.save_clean_gif` | Reused unchanged |
+| `util.gif_utils.save_clean_gif` | Added an explicit `opaque=True` mode for pre-composited frames; transparent default remains unchanged |
| `video.loop.verify_animation` | Verifies every frame's opacity and corner color only with its explicit background parameter; its default transparent corner check and loop checks are unchanged |
| `write_webp` | Reused unchanged |
diff --git a/sprite_gen/util/gif_utils.py b/sprite_gen/util/gif_utils.py
index b937bc4..7496c14 100644
--- a/sprite_gen/util/gif_utils.py
+++ b/sprite_gen/util/gif_utils.py
@@ -60,7 +60,9 @@ def save_clean_gif(
if duration_ms <= 0:
raise ValueError("duration_ms must be positive")
if opaque:
- prepared = [frame.convert("RGB").convert("P", palette=Image.ADAPTIVE) for frame in frames]
+ # Sparse highlights need palette coverage even beside a large glow body.
+ prepared = [frame.convert("RGB").quantize(colors=256, method=Image.Quantize.MAXCOVERAGE)
+ for frame in frames]
for frame in prepared:
frame.info.pop("transparency", None)
# Restore-to-background depends on palette indices that can change per
diff --git a/tests/util/test_gif_utils.py b/tests/util/test_gif_utils.py
index 3e10121..7fbde4c 100644
--- a/tests/util/test_gif_utils.py
+++ b/tests/util/test_gif_utils.py
@@ -4,6 +4,7 @@ from PIL import Image
import pytest
from sprite_gen.util.gif_utils import save_clean_gif
+from sprite_gen._deps import np
@pytest.mark.parametrize("loop", [None, 0])
@@ -54,3 +55,17 @@ def test_default_transparency_clears_moving_subject_and_preserves_playback(tmp_p
assert preview.convert("RGBA").tobytes() == expected.tobytes(), k
assert preview.disposal_method == 2
assert preview.info["duration"] == 70
+
+
+def test_opaque_palette_retains_a_rare_light_colour(tmp_path):
+ y, x = np.mgrid[:128, :128]
+ rgb = np.stack([x * 2, y * 2, np.zeros_like(x)], axis=2).astype(np.uint8)
+ rgb[:64, :, 2] = rgb[:64, :, 1]
+ rgb[-2, -2] = (128, 128, 200)
+ path = tmp_path / "light.gif"
+ save_clean_gif([Image.fromarray(rgb)], path, duration_ms=60, opaque=True)
+
+ with Image.open(path) as preview:
+ error = np.abs(np.asarray(preview.convert("RGB"), dtype=int) - rgb)
+ assert error[-2, -2].max() <= 16
+ assert error.mean() < 4