Skip to content

Fix adding a new color when image palette has missing background or transparency index#9799

Merged
radarhere merged 3 commits into
python-pillow:mainfrom
chuenchen309:fix/imagepalette-getcolor-special-skip-index
Jul 19, 2026
Merged

Fix adding a new color when image palette has missing background or transparency index#9799
radarhere merged 3 commits into
python-pillow:mainfrom
chuenchen309:fix/imagepalette-getcolor-special-skip-index

Conversation

@chuenchen309

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • Fix ImagePalette.getcolor() silently losing a color when a background/transparency index is skipped: _new_color_index() advances the index past the palette tail to avoid a reserved slot, but the append branch stored the color at the current tail while returning the advanced index — so the returned index addressed a nonexistent or reserved slot and the requested color was lost.
  • Pad the skipped reserved slots so the color lands at the returned index (a no-op when no slot is skipped).
  • Add a regression test asserting the returned index actually addresses the stored color, for both an empty and a full palette.

Reachable from the public API: putpixel((x, y), (r, g, b)) on a P/PA image whose transparency/background index equals the next free palette slot, and ImageDraw fill/text on P images, both route through getcolor(). Completes the intent of #5564, which added the reserved-index skip but did not store the color at the skipped-to index.


This PR was authored by an AI coding agent (Claude Code) running on this account: the AI found the bug, reproduced it end-to-end through putpixel, wrote the test, and wrote this description. The human account holder reviews every change and is accountable for it. The verification is real and re-runnable from the diff (the palette/getcolor call-site test files pass). If this isn't the kind of contribution you want, say so and I'll close it.

When getcolor allocates a new entry, _new_color_index advances the index
past the palette tail to skip a slot reserved for the background or
transparency index. The append branch then wrote the color at the current
tail but returned the advanced index, so the returned index addressed a
slot that either did not exist or held a different (reserved) color, and
the requested color was silently lost.

Pad the skipped reserved slots so the color is stored at the index that
is returned. This is a no-op when no slot was skipped.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Andrew Chen <48723787+chuenchen309@users.noreply.github.com>
@radarhere radarhere changed the title Store the color at the returned index in ImagePalette.getcolor Fix adding a new color when image palette has missing background or transparency index Jul 19, 2026
@radarhere
radarhere merged commit 8f86212 into python-pillow:main Jul 19, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants