Skip to content

Fix ImagePalette colors for raw palettes#9821

Open
nightcityblade wants to merge 2 commits into
python-pillow:mainfrom
nightcityblade:fix/issue-9820
Open

Fix ImagePalette colors for raw palettes#9821
nightcityblade wants to merge 2 commits into
python-pillow:mainfrom
nightcityblade:fix/issue-9820

Conversation

@nightcityblade

Copy link
Copy Markdown

Fixes #9820.

Changes proposed in this pull request:

  • Convert raw palette data through Pillow's existing palette conversion path before constructing ImagePalette.colors.
  • Preserve the correct channel order and entry width for raw modes such as BGRX.
  • Add a regression test covering the five-color BGRX palette from the issue.

Tests:

  • python -m pytest Tests/test_imagepalette.py Tests/test_file_bmp.py Tests/test_image_getpalette.py Tests/test_image_putpalette.py -q (64 passed)
  • ruff check --no-fix src/PIL/ImagePalette.py Tests/test_imagepalette.py
  • ruff format --check src/PIL/ImagePalette.py Tests/test_imagepalette.py
  • black --check src/PIL/ImagePalette.py Tests/test_imagepalette.py
  • mypy --python-executable .venv/bin/python src/PIL/ImagePalette.py Tests/test_imagepalette.py

This change was prepared by an AI coding agent. I reviewed the focused diff and verified the issue reproducer and tests locally.

Comment thread src/PIL/ImagePalette.py Outdated
Comment on lines +69 to +73
image = Image.new("P", (1, 1))
image.putpalette(palette, self.rawmode)
converted_palette = image.getpalette(self.mode)
assert converted_palette is not None
palette = converted_palette

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
image = Image.new("P", (1, 1))
image.putpalette(palette, self.rawmode)
converted_palette = image.getpalette(self.mode)
assert converted_palette is not None
palette = converted_palette
im = Image.core.new("P", (0, 0))
im.putpalette(self.mode, self.rawmode, palette)
palette = im.getpalette(self.mode, self.mode)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 84571ab. The raw palette now uses the core image conversion path from your suggestion. The focused palette/BMP/getpalette/putpalette suite passes (64 tests), and Ruff check/format both pass.

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.

ImagePalette misinterprets BGRX as RGB

2 participants