Skip to content

Return palette contents in suitable format for C putpalette() - #9985

Open
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:bytes
Open

Return palette contents in suitable format for C putpalette()#9985
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:bytes

Conversation

@radarhere

Copy link
Copy Markdown
Member

ImagePalette.getdata() does not always fulfil its described purpose.

def getdata(self) -> tuple[str, Sequence[int] | bytes | bytearray]:
"""
Get palette contents in format suitable for the low-level
``im.putpalette`` primitive.

from PIL import Image, ImagePalette
im = Image.new("P", (1, 1))
palette = ImagePalette.raw("RGB", list(range(256)) * 3)
rawmode, data_out = palette.getdata()
im.im.putpalette("RGB", rawmode, data_out)

currently raises

Traceback (most recent call last):
  File "demo.py", line 5, in <module>
    im.im.putpalette("RGB", rawmode, data_out)
TypeError: a bytes-like object is required, not 'list'

This PR ensures that the palette data is always returned as bytes.

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