#1886·skills

slack-gif-creator: add_frame() never converts ndarray frames to RGB, so RGBA/greyscale input crashes save()

Author: sxh313Created Sep 27, 2026Updated Sep 27, 2026

Summary

GIFBuilder.add_frame() is typed np.ndarray | Image.Image and its docstring promises the argument "will be converted to RGB" (core/gif_builder.py:34,39). Only the PIL.Image branch converts:

python
if isinstance(frame, Image.Image):          # :41
    frame = np.array(frame.convert("RGB"))  # :42
# an ndarray falls through to :45 with its original channel count

A numpy array is therefore stored as-is, and optimize_colors() assumes three channels per pixel in two places — :86 np.vstack([f.reshape(-1, 3) for f in sample_frames]) and :113 pil_frame.quantize(palette=global_palette, dither=1), and palette quantising accepts only RGB/L. So save() raises from 45–130 lines below the call the caller actually made, with a message that never mentions the frame, the channel count, or alpha. deduplicate_frames() is a third site for the same missing normalisation: :147 np.abs(prev_frame - curr_frame) cannot subtract an (h,w,3) frame from an (h,w,4) one.

The resize branch just below (:45-50) calls Image.fromarray(frame) on ndarrays, which is proof the array path is meant to be supported — it just never reaches the .convert("RGB").

Measured on main (3337550), Windows, Python 3.14.6 / Pillow 12.3.0 / numpy 2.5.3 / imageio 2.37.4

The same 10-frame animation (light-blue field, red disc sliding down) drawn once per input form, then save(..., num_colors=48). sha is the first 12 hex chars of the written GIF.

input to add_frame() before after
PIL RGBA 120x120 — documented workflow, the reference ok sha=b2c1b9f8449c ok sha=b2c1b9f8449c
numpy RGB 120x120 ok sha=b2c1b9f8449c ok sha=b2c1b9f8449c
PIL RGB 120x120 ok sha=b2c1b9f8449c ok sha=b2c1b9f8449c
numpy RGBA 128x128 ValueError: cannot reshape array of size 65536 into shape (3) ok 10 frames
numpy RGBA 120x120 ValueError: only RGB or L mode images can be quantized to a palette ok sha=b2c1b9f8449c — byte-identical to the PIL RGBA reference
numpy L (greyscale) 128x128 ValueError: cannot reshape array of size 16384 into shape (3) ok 10 frames
PIL RGB + numpy RGBA + numpy RGB, remove_duplicates=True ValueError: operands could not be broadcast together with shapes (128,128,3) (128,128,4) ok
numpy RGBA 480x480 + optimize_for_emoji=True ValueError: cannot reshape array of size 65536 into shape (3) ok sha=87255aa65958 — byte-identical to the same sequence passed as RGB
numpy RGBA, one frame only ok sha=a5d2af8d6219 ok sha=a5d2af8d6219 (unchanged)

Two things stand out:

  1. The same input produces two different errors, decided only by the frame size. 128*128*4 = 65536 is not a multiple of 3, so the reshape(-1, 3) throws; but 120*120*4 = 57600 = 3*19200, so the reshape "succeeds" on byte triplets that straddle pixel boundaries and the failure moves one statement later to quantize(). Nothing in either message names alpha, channels, or the offending frame, so the caller has no way to connect it to add_frame().
  2. A single RGBA frame works today. optimize_colors() only builds a global palette when len(self.frames) > 1, and the per-frame branch (:119 quantize(colors=...)) does accept RGBA. So the module already accepts RGBA arrays — it just fails to normalise them, and adding a second frame turns acceptance into a crash. That inconsistency is also what makes this worth fixing in add_frame() rather than by narrowing the docstring.

The four rows that already worked are byte-identical before and after, including the previously-working one-frame RGBA case.

Reproduction

$ git rev-parse HEAD        # 33375500bcea98d610eb30ce10ac4e59b89c390d

Save this at the repo root and run it against main, then against the fix:

python
import sys
import numpy as np
from PIL import Image, ImageDraw

sys.path.insert(0, "skills/slack-gif-creator")
from core.gif_builder import GIFBuilder

BG, DISK = (240, 248, 255), (220, 40, 40)


def frames(wh, mode, n=10):
    bg, disk = {"RGBA": (BG + (255,), DISK + (255,)),
                "RGB": (BG, DISK),
                "L": (235, 60)}[mode]
    out = []
    for i in range(n):
        im = Image.new(mode, (wh, wh), bg)
        ImageDraw.Draw(im).ellipse([wh // 4, wh // 4 + i * 2,
                                    wh * 3 // 4, wh * 3 // 4 + i * 2], fill=disk)
        out.append(im)
    return out


def attempt(tag, mode, wh, n=10, as_array=True, mixed=False, dedup=False):
    b = GIFBuilder(wh, wh, 10)
    try:
        if mixed:
            b.add_frame(frames(128, "RGB", 1)[0])              # PIL RGB
            b.add_frame(np.array(frames(128, "RGBA", 1)[0]))   # numpy RGBA
            b.add_frame(np.array(frames(128, "RGB", 1)[0]))    # numpy RGB
        else:
            b.add_frames([np.array(f) if as_array else f for f in frames(wh, mode, n)])
        info = b.save("_repro.gif", num_colors=48, remove_duplicates=dedup)
        print("%-34s OK   frames=%d" % (tag, info["frame_count"]))
    except Exception as e:
        print("%-34s FAIL %s: %s" % (tag, type(e).__name__, e))


attempt("PIL RGBA 120x120 (reference)", "RGBA", 120, as_array=False)
attempt("numpy RGB 120x120", "RGB", 120)
attempt("numpy RGBA 128x128", "RGBA", 128)
attempt("numpy RGBA 120x120", "RGBA", 120)
attempt("numpy grey 128x128", "L", 128)
attempt("numpy RGBA, 1 frame only", "RGBA", 128, n=1)
attempt("mixed RGB/RGBA ndarray", "RGBA", 128, mixed=True, dedup=True)

Before (core/gif_builder.py at 3337550):

Python 3.14.6
module: ...\red\core\gif_builder.py
PIL RGBA 120x120 (reference)       OK   frames=10
numpy RGB 120x120                  OK   frames=10
numpy RGBA 128x128                 FAIL ValueError: cannot reshape array of size 65536 into shape (3)
numpy RGBA 120x120                 FAIL ValueError: only RGB or L mode images can be quantized to a palette
numpy grey 128x128                 FAIL ValueError: cannot reshape array of size 16384 into shape (3)
numpy RGBA, 1 frame only           OK   frames=1
mixed RGB/RGBA ndarray             FAIL ValueError: operands could not be broadcast together with shapes (128,128,3) (128,128,4)

After the proposed change:

Python 3.14.6
module: ...\anthro-skills\skills\slack-gif-creator\core\gif_builder.py
PIL RGBA 120x120 (reference)       OK   frames=10
numpy RGB 120x120                  OK   frames=10
numpy RGBA 128x128                 OK   frames=10
numpy RGBA 120x120                 OK   frames=10
numpy grey 128x128                 OK   frames=10
numpy RGBA, 1 frame only           OK   frames=1
mixed RGB/RGBA ndarray             OK   frames=1

The RGBA failure is not hypothetical for this skill's own guidance: SKILL.md:187-192 ("Fade In/Out") tells the reader to "Create RGBA image, adjust alpha channel", and add_frame's signature is the advertised way to hand the result to the builder.

Expected

add_frame() should keep the promise its docstring already makes for both advertised input types, so that every stored frame is (height, width, 3) before optimize_colors() or deduplicate_frames() see it. The normalisation belongs in add_frame() — not in optimize_colors() — because that is where the documented conversion is promised and it is the only place that also fixes the dedup broadcast.

Why the shape test is frame.shape[-1:] != (3,)

It is the same check the next statement already performs on the other axis (:45 frame.shape[:2] != (self.height, self.width)), it leaves a genuine RGB array untouched with no copy, and using Image.fromarray(...).convert("RGB") makes the ndarray path go through the identical conversion the PIL path uses at :42 — which is why the fixed RGBA row above is byte-identical to the PIL RGBA reference rather than merely similar.

Linked fix

PR #1887 : 4 added lines in skills/slack-gif-creator/core/gif_builder.py, no other file, no behaviour change for any input that works today.

Dedup

Checked by patch content, not just file lists. Every one of the 956 currently-open PRs has a changed-file list on record: 899 from the per-file census taken 2026-09-23 (numbers up to #1804), 49 open PRs created since 2026-09-23T14:00Z fetched live, and the remaining 9 open PRs that appear in neither set (#1322 #1387 #1422 #1445 #1451 #1453 #1456 #1457 #1472) fetched live — none of those 9 touches skills/slack-gif-creator at all.

The complete set of open PRs whose files include core/gif_builder.py is therefore:

PR what its patch does to the file overlaps add_frame (:34-52)?
#771 @@ -7,7 +7,6 @@ — deletes the unused from typing import Optional no
#785 same one-line import deletion no
#1814 (mine) @@ -211,11 +211,9 @@ — the optimize_for_emoji frame sampling no
#1871 (mine) @@ -220,8 +220,10 @@ / @@ -242,7 +244,7 @@ — frame_duration and duration_seconds no

No open PR or issue reports this: gh search issues --repo anthropics/skills --state open for add_frame, converted to RGB and RGBA returns 0 / 0 / 0 hits, and optimize_colors returns 1 (#1813, mine, about frame sampling).