diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index bdec8273d..a79ae8d9f 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -153,6 +153,26 @@ class TestFileWebp(PillowTestCase): Image.open(blob).load() Image.open(blob).load() + @unittest.skipUnless(HAVE_WEBP and _webp.HAVE_WEBPANIM, + "WebP save all not available") + def test_background_from_gif(self): + im = Image.open("Tests/images/chi.gif") + original_value = im.convert("RGB").getpixel((1, 1)) + + # Save as WEBP + out_webp = self.tempfile("temp.webp") + im.save(out_webp, save_all=True) + + # Save as GIF + out_gif = self.tempfile("temp.gif") + Image.open(out_webp).save(out_gif) + + reread = Image.open(out_gif) + reread_value = reread.convert("RGB").getpixel((1, 1)) + difference = sum([abs(original_value[i] - reread_value[i]) + for i in range(0, 3)]) + self.assertLess(difference, 5) + if __name__ == '__main__': unittest.main() diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py index 084eedf53..212e6b4c7 100644 --- a/src/PIL/WebPImagePlugin.py +++ b/src/PIL/WebPImagePlugin.py @@ -196,6 +196,14 @@ def _save_all(im, fp, filename): background = encoderinfo["background"] elif "background" in im.info: background = im.info["background"] + if isinstance(background, int): + # GifImagePlugin stores a global color table index in + # info["background"]. So it must be converted to an RGBA value + palette = im.getpalette() + if palette: + r, g, b = palette[background*3:(background+1)*3] + background = (r, g, b, 0) + duration = im.encoderinfo.get("duration", 0) loop = im.encoderinfo.get("loop", 0) minimize_size = im.encoderinfo.get("minimize_size", False)