From 4e60811bda5d2f1eb929a8ce87dcc854bc649b24 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 21 Mar 2022 20:09:53 +1100 Subject: [PATCH] Fixed saving L mode GIF with background as WebP --- Tests/images/no_palette_with_background.gif | Bin 0 -> 54 bytes Tests/test_file_webp.py | 6 ++++++ src/PIL/WebPImagePlugin.py | 4 +++- 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Tests/images/no_palette_with_background.gif diff --git a/Tests/images/no_palette_with_background.gif b/Tests/images/no_palette_with_background.gif new file mode 100644 index 0000000000000000000000000000000000000000..e49e5d461aabc5f69ecbe79fc46ce24523d6e735 GIT binary patch literal 54 ocmZ?wbh9u|WMp7uX!y?n1dNP~I$#;M1& literal 0 HcmV?d00001 diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 0511193782c..7a9999870fd 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -174,6 +174,12 @@ def test_file_pointer_could_be_reused(self): @skip_unless_feature("webp") @skip_unless_feature("webp_anim") def test_background_from_gif(self, tmp_path): + # Save L mode GIF with background + with Image.open("Tests/images/no_palette_with_background.gif") as im: + out_webp = str(tmp_path / "temp.webp") + im.save(out_webp, save_all=True) + + # Save P mode GIF with background with Image.open("Tests/images/chi.gif") as im: original_value = im.convert("RGB").getpixel((1, 1)) diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py index 590161f3ecc..135c400e5e4 100644 --- a/src/PIL/WebPImagePlugin.py +++ b/src/PIL/WebPImagePlugin.py @@ -190,7 +190,9 @@ def _save_all(im, fp, filename): palette = im.getpalette() if palette: r, g, b = palette[background * 3 : (background + 1) * 3] - background = (r, g, b, 0) + background = (r, g, b, 255) + else: + background = (background, background, background, 255) duration = im.encoderinfo.get("duration", im.info.get("duration")) loop = im.encoderinfo.get("loop", 0)