diff --git a/Tests/images/no_palette_with_background.gif b/Tests/images/no_palette_with_background.gif new file mode 100644 index 00000000000..e49e5d461aa Binary files /dev/null and b/Tests/images/no_palette_with_background.gif differ diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 13fbb529142..c69e13a890e 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -187,6 +187,12 @@ def test_file_pointer_could_be_reused(self): @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 370b44e3a32..7dd3f527211 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", 0)) loop = im.encoderinfo.get("loop", 0)