From a682ceaf47abbe28dc70c6bd4aab06f8f3f4ac90 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 13 Jul 2023 15:20:44 +1000 Subject: [PATCH] Do not use transparency if it has been removed when normalizing mode --- Tests/test_file_gif.py | 15 +++++++++++++++ src/PIL/GifImagePlugin.py | 6 +----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index f4a17264f..b1c9f731f 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1086,6 +1086,21 @@ def test_transparent_optimize(tmp_path): assert reloaded.info["transparency"] == reloaded.getpixel((252, 0)) +def test_removed_transparency(tmp_path): + out = str(tmp_path / "temp.gif") + im = Image.new("RGB", (256, 1)) + + for x in range(256): + im.putpixel((x, 0), (x, 0, 0)) + + im.info["transparency"] = (255, 255, 255) + with pytest.warns(UserWarning): + im.save(out) + + with Image.open(out) as reloaded: + assert "transparency" not in reloaded.info + + def test_rgb_transparency(tmp_path): out = str(tmp_path / "temp.gif") diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index cf2993e38..255643de6 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -683,11 +683,7 @@ def get_interlace(im): def _write_local_header(fp, im, offset, flags): transparent_color_exists = False try: - if "transparency" in im.encoderinfo: - transparency = im.encoderinfo["transparency"] - else: - transparency = im.info["transparency"] - transparency = int(transparency) + transparency = int(im.encoderinfo["transparency"]) except (KeyError, ValueError): pass else: