diff --git a/Tests/images/background_outside_palette.gif b/Tests/images/background_outside_palette.gif new file mode 100644 index 00000000000..63e767463c4 Binary files /dev/null and b/Tests/images/background_outside_palette.gif differ diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index fa5d54febf8..d18ce205539 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1142,6 +1142,12 @@ def test_rgba_transparency(tmp_path): assert_image_equal(hopper("P").convert("RGB"), reloaded) +def test_background_outside_palettte(tmp_path): + with Image.open("Tests/images/background_outside_palette.gif") as im: + im.seek(1) + assert im.info["background"] == 255 + + def test_bbox(tmp_path): out = str(tmp_path / "temp.gif") diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 4ce295f7f21..d30dfbb3d4f 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -335,6 +335,8 @@ def _seek(self, frame, update_image=True): def _rgb(color): if self._frame_palette: + if color * 3 + 3 > len(self._frame_palette.palette): + color = 0 color = tuple(self._frame_palette.palette[color * 3 : color * 3 + 3]) else: color = (color, color, color)