Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Tests/test_imagepalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ def test_getcolor_not_special(index: int, palette: ImagePalette.ImagePalette) ->

# Do not use transparency index as a new color
im.info["transparency"] = index
index1 = palette.getcolor((0, 0, 0), im)
index1 = palette.getcolor((0, 0, 1), im)
assert index1 != index
roundtripped_palette = ImagePalette.ImagePalette(palette=palette.palette)
assert roundtripped_palette.colors[(0, 0, 1)] == index1

# Do not use background index as a new color
im.info["background"] = index1
index2 = palette.getcolor((0, 0, 1), im)
index2 = palette.getcolor((0, 0, 2), im)
assert index2 not in (index, index1)
roundtripped_palette = ImagePalette.ImagePalette(palette=palette.palette)
assert roundtripped_palette.colors[(0, 0, 2)] == index2


def test_file(tmp_path: Path) -> None:
Expand Down
4 changes: 4 additions & 0 deletions src/PIL/ImagePalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ def _new_color_index(
image.info.get("background"),
image.info.get("transparency"),
)
assert isinstance(self._palette, bytearray)
while index in special_colors:
# Background or transparency index points past the end of the palette.
# Set it to black, so that the new color can be written afterwards.
self._palette += bytearray(len(self.mode))
index += 1
if index >= 256:
if image:
Expand Down
Loading