Only update Python palette when loading an image if rawmode was different (#9309)

This commit is contained in:
Andrew Murray 2026-01-04 06:20:56 +11:00 committed by GitHub
commit a85d3b135d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -68,10 +68,22 @@ def test_sanity() -> None:
draw.rectangle(list(range(4)))
def test_valueerror() -> None:
def test_new_color() -> None:
with Image.open("Tests/images/chi.gif") as im:
draw = ImageDraw.Draw(im)
assert im.palette is not None
assert len(im.palette.colors) == 249
# Test drawing a new color onto the palette
draw.line((0, 0), fill=(0, 0, 0))
assert im.palette is not None
assert len(im.palette.colors) == 250
assert im.palette.dirty
# Test drawing another new color, now that the palette is dirty
draw.point((0, 0), fill=(1, 0, 0))
assert len(im.palette.colors) == 251
assert im.convert("RGB").getpixel((0, 0)) == (1, 0, 0)
def test_mode_mismatch() -> None:

View File

@ -892,7 +892,9 @@ class Image:
else:
self.im.putpalettealphas(self.info["transparency"])
self.palette.mode = "RGBA"
else:
elif self.palette.mode != mode:
# If the palette rawmode is different to the mode,
# then update the Python palette data
self.palette.palette = self.im.getpalette(
self.palette.mode, self.palette.mode
)