Skip to content

Do not treat RGB as a rawmode for an RGB palette - #10002

Open
radarhere wants to merge 2 commits into
python-pillow:mainfrom
radarhere:palette
Open

radarhere wants to merge 2 commits into
python-pillow:mainfrom
radarhere:palette

Conversation

@radarhere

@radarhere radarhere commented Sep 15, 2026

Copy link
Copy Markdown
Member

At the moment, when creating a raw ImagePalette with an RGB rawmode,

>>> from PIL import ImagePalette
>>> palette = ImagePalette.raw("RGB", [0, 1, 2])
>>> palette.tobytes()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/ImagePalette.py", line 124, in tobytes
    raise ValueError(msg)
ValueError: palette contains raw palette data

Oh, I'll have to do something so that this 'raw palette data' can be decoded into... wait a minute.

>>> palette.mode
'RGB'

The rawmode and mode are the same! Who needs to decode anything?

ImagePalette.raw() creates an ImagePalette in an RGB mode.

def raw(rawmode: str, data: Sequence[int] | bytes | bytearray) -> ImagePalette:
palette = ImagePalette()

def __init__(
self,
mode: str = "RGB",

If the rawmode is also in RGB mode, then instead of setting rawmode on the palette and potentially raising an error later, let's simply not set rawmode.

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.

1 participant