Skip to content

Fix ImagePalette colors for raw palettes#9821

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

Fix ImagePalette colors for raw palettes#9821
nightcityblade wants to merge 3 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 thread Tests/test_imagepalette.py Outdated
def test_colors_rawmode() -> None:
palette = ImagePalette.raw(
"BGRX",
bytes.fromhex("00000000ffffff000000ff0000ff0000ff000000"),

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
bytes.fromhex("00000000ffffff000000ff0000ff0000ff000000"),
"BGRX", (0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0)

This makes it much easier to read, swap around the channels in your head, and understand how the BGRX values are transformed into the expected RGB values.

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 b3bbfe5. The test now uses explicit channel values as suggested, and the core conversion normalizes sequence input to bytes. Local validation: all 64 focused palette/BMP/getpalette/putpalette tests passed; Ruff check and format passed on both changed files. Thanks.

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