From 46a80d144a16836af304a7aaa8e620962d91ac23 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 21 May 2022 16:35:01 +1000 Subject: [PATCH] Update transparency when remapping the palette --- Tests/test_file_gif.py | 28 ++++++++++++++++++++++------ src/PIL/GifImagePlugin.py | 2 +- src/PIL/Image.py | 3 +++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 9caea84a8..49dc3d841 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -620,6 +620,7 @@ def test_dispose2_background(tmp_path): def test_transparency_in_second_frame(tmp_path): + out = str(tmp_path / "temp.gif") with Image.open("Tests/images/different_transparency.gif") as im: assert im.info["transparency"] == 0 @@ -629,14 +630,13 @@ def test_transparency_in_second_frame(tmp_path): assert_image_equal_tofile(im, "Tests/images/different_transparency_merged.png") - out = str(tmp_path / "temp.gif") im.save(out, save_all=True) - with Image.open(out) as reread: - reread.seek(reread.tell() + 1) - assert_image_equal_tofile( - reread, "Tests/images/different_transparency_merged.png" - ) + with Image.open(out) as reread: + reread.seek(reread.tell() + 1) + assert_image_equal_tofile( + reread, "Tests/images/different_transparency_merged.png" + ) def test_no_transparency_in_second_frame(): @@ -649,6 +649,22 @@ def test_no_transparency_in_second_frame(): assert img.histogram()[255] == 0 +def test_remapped_transparency(tmp_path): + out = str(tmp_path / "temp.gif") + + im = Image.new("P", (1, 2)) + im2 = im.copy() + + # Add transparency at a higher index + # so that it will be optimized to a lower index + im.putpixel((0, 1), 5) + im.info["transparency"] = 5 + im.save(out, save_all=True, append_images=[im2]) + + with Image.open(out) as reloaded: + assert reloaded.info["transparency"] == reloaded.getpixel((0, 1)) + + def test_duration(tmp_path): duration = 1000 diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index fad6cdf1b..cead48c19 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -578,9 +578,9 @@ def _write_multiple_frames(im, fp, palette): im.encoderinfo.setdefault(k, v) encoderinfo = im.encoderinfo.copy() + im_frame = _normalize_palette(im_frame, palette, encoderinfo) if "transparency" in im_frame.info: encoderinfo.setdefault("transparency", im_frame.info["transparency"]) - im_frame = _normalize_palette(im_frame, palette, encoderinfo) if isinstance(duration, (list, tuple)): encoderinfo["duration"] = duration[frame_count] if isinstance(disposal, (list, tuple)): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 813ac52aa..086b2b196 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1934,6 +1934,9 @@ class Image: m_im.putpalette(new_palette_bytes) m_im.palette = ImagePalette.ImagePalette("RGB", palette=palette_bytes) + if "transparency" in self.info: + m_im.info["transparency"] = new_positions[self.info["transparency"]] + return m_im def _get_safe_box(self, size, resample, box):