Change tuple background to global color table index when saving

This commit is contained in:
Andrew Murray 2018-10-02 20:52:07 +10:00
parent 3437d5fcb4
commit a7eb2ea9cc
2 changed files with 20 additions and 2 deletions

View File

@ -4,6 +4,12 @@ from PIL import Image, ImagePalette, GifImagePlugin
from io import BytesIO
try:
from PIL import _webp
HAVE_WEBP = True
except ImportError:
HAVE_WEBP = False
codecs = dir(Image.core)
# sample gif stream
@ -411,6 +417,11 @@ class TestFileGif(PillowTestCase):
self.assertEqual(reread.info['background'], im.info['background'])
if HAVE_WEBP and _webp.HAVE_WEBPANIM:
im = Image.open("Tests/images/hopper.webp")
self.assertIsInstance(im.info['background'], tuple)
im.save(out)
def test_comment(self):
im = Image.open(TEST_GIF)
self.assertEqual(im.info['comment'],

View File

@ -711,11 +711,18 @@ def _get_global_header(im, info):
if im.info.get("version") == b"89a":
version = b"89a"
background = 0
if "background" in info:
background = info["background"]
if isinstance(background, tuple):
# WebPImagePlugin stores an RGBA value in info["background"]
# So it must be converted to the same format as GifImagePlugin's
# info["background"] - a global color table index
background = im.palette.getcolor(background)
palette_bytes = _get_palette_bytes(im)
color_table_size = _get_color_table_size(palette_bytes)
background = info["background"] if "background" in info else 0
return [
b"GIF"+version + # signature + version
o16(im.size[0]) + # canvas width