From 7c2b6811834ec36846d9bf9dc97675eecc777dd8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 2 Nov 2023 16:05:13 +1100 Subject: [PATCH] Handle disposing background from outside palette --- Tests/images/background_outside_palette.gif | Bin 0 -> 82 bytes Tests/test_file_gif.py | 6 ++++++ src/PIL/GifImagePlugin.py | 2 ++ 3 files changed, 8 insertions(+) create mode 100644 Tests/images/background_outside_palette.gif diff --git a/Tests/images/background_outside_palette.gif b/Tests/images/background_outside_palette.gif new file mode 100644 index 0000000000000000000000000000000000000000..63e767463c498b85ab40023298e1434e76db4598 GIT binary patch literal 82 ycmZ?wbh9u|WMp7uX!y?n0g69aI6y2N5CM{5;9y~3WMN^zkZ6Qy0jY#4vIYQ>R|mBK literal 0 HcmV?d00001 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)