Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Only update Python palette when loading an image if rawmode was different

Open radarhere opened this issue 1 month ago • 1 comments

After loading the palette into the C image in load(), https://github.com/python-pillow/Pillow/blob/ec40c546d7d38efebcb228745291bd3ba6233196/src/PIL/Image.py#L889-L891 the Python palette may also be updated with what the C image now reports. https://github.com/python-pillow/Pillow/blob/ec40c546d7d38efebcb228745291bd3ba6233196/src/PIL/Image.py#L901-L903

This PR suggests only updating the Python palette when the palette rawmode is different to the mode. The rest of the time, the palette data will be unchanged by the trip through the C layer, and this is redundant.

radarhere avatar Nov 25 '25 12:11 radarhere

This is an alternative fix to #9308. When the Python palette was updated, it was being set to bytes, and failing a type hint assertion.

I've updated a test from https://github.com/python-pillow/Pillow/pull/1985 to cover this scenario in our test suite.

radarhere avatar Nov 26 '25 09:11 radarhere