Change color table index background to tuple when saving

This commit is contained in:
Andrew Murray 2018-11-20 19:50:14 +11:00
parent 84df069169
commit 6c126ca282
2 changed files with 28 additions and 0 deletions

View File

@ -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()

View File

@ -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)