Merge pull request #3471 from radarhere/background

Change color table index background to tuple when saving as WebP
This commit is contained in:
Hugo 2018-12-04 23:22:53 +02:00 committed by GitHub
commit 231604e921
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 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

@ -191,7 +191,19 @@ def _save_all(im, fp, filename):
_save(im, fp, filename)
return
background = encoderinfo.get("background", (0, 0, 0, 0))
background = (0, 0, 0, 0)
if "background" in encoderinfo:
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)