diff --git a/Tests/test_imagepalette.py b/Tests/test_imagepalette.py index 526beb656e8..3f37c23c5ae 100644 --- a/Tests/test_imagepalette.py +++ b/Tests/test_imagepalette.py @@ -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: diff --git a/src/PIL/ImagePalette.py b/src/PIL/ImagePalette.py index 2abbd46eaf1..03fb4d8c4a4 100644 --- a/src/PIL/ImagePalette.py +++ b/src/PIL/ImagePalette.py @@ -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: