Skip to content

Do not load palette in ImageFile load_prepare() - #10268

Merged
radarhere merged 3 commits into
python-pillow:mainfrom
radarhere:load_prepare
Oct 22, 2026
Merged

radarhere merged 3 commits into
python-pillow:mainfrom
radarhere:load_prepare

Conversation

@radarhere

@radarhere radarhere commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Pillow/src/PIL/ImageFile.py

Lines 462 to 468 in 60dcd68

def load_prepare(self) -> None:
# create image memory if necessary
if self._im is None:
self.im = Image.core.new(self.mode, self.size)
# create palette (optional)
if self.mode == "P":
Image.Image.load(self)

This "create palette (optional)" code has been there since PIL. The effect would be to call Image.load() and potentially copy the palette from Python to C

Pillow/src/PIL/Image.py

Lines 984 to 987 in 60dcd68

if self._im is not None and self.palette and self.palette.dirty:
# realize palette
mode, arr = self.palette.getdata()
self.im.putpalette(self.palette.mode, mode, arr)

At minimum, it would be better to change the ImageFile condition to match the self.palette.dirty condition.

However, I don't think the palette needs to be copied here at all. None of our decoders use the C palette. It would be surprising if they read P data only to immediately transform it. A dirty palette can be left for the end of ImageFile's load() to handle.

return Image.Image.load(self)

So I suggest removing the call to Image.Image.load(self).

@radarhere
radarhere merged commit 7aa76ba into python-pillow:main Oct 22, 2026
51 of 52 checks passed
@radarhere
radarhere deleted the load_prepare branch October 22, 2026 22:13
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