Merge pull request #1896 from radarhere/comment

Added support for GIF comment extension
This commit is contained in:
wiredfool 2016-05-23 22:32:52 +01:00
commit 1eedd2ef15
2 changed files with 29 additions and 4 deletions

View File

@ -198,6 +198,11 @@ class GifImageFile(ImageFile.ImageFile):
# correct, but it seems to prevent the last
# frame from looking odd for some animations
self.disposal_method = dispose_bits
elif i8(s) == 254:
#
# comment extension
#
self.info["comment"] = block
elif i8(s) == 255:
#
# application extension
@ -455,6 +460,12 @@ def _get_local_header(fp, im, offset, flags):
o8(transparency) + # transparency index
o8(0))
if "comment" in im.encoderinfo and 1 <= len(im.encoderinfo["comment"]) <= 255:
fp.write(b"!" +
o8(254) + # extension intro
o8(len(im.encoderinfo["comment"])) +
im.encoderinfo["comment"] +
o8(0))
if "loop" in im.encoderinfo:
number_of_loops = im.encoderinfo["loop"]
fp.write(b"!" +
@ -547,9 +558,11 @@ def getheader(im, palette=None, info=None):
# http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
version = b"87a"
for extensionKey in ["transparency", "duration", "loop"]:
if info and extensionKey in info and \
not (extensionKey == "duration" and info[extensionKey] == 0):
for extensionKey in ["transparency", "duration", "loop", "comment"]:
if info and extensionKey in info:
if ((extensionKey == "duration" and info[extensionKey] == 0) or
(extensionKey == "comment" and not (1 <= len(info[extensionKey]) <= 255))):
continue
version = b"89a"
break
else:

View File

@ -267,6 +267,18 @@ class TestFileGif(PillowTestCase):
self.assertEqual(reread.info['background'], im.info['background'])
def test_comment(self):
im = Image.open(TEST_GIF)
self.assertEqual(im.info['comment'], b"File written by Adobe Photoshop\xa8 4.0")
out = self.tempfile('temp.gif')
im = Image.new('L', (100, 100), '#000')
im.info['comment'] = b"Test comment text"
im.save(out)
reread = Image.open(out)
self.assertEqual(reread.info['comment'], im.info['comment'])
def test_version(self):
out = self.tempfile('temp.gif')
@ -283,7 +295,7 @@ class TestFileGif(PillowTestCase):
self.assertEqual(reread.info["version"], b"GIF89a")
# Test that a GIF87a image is also saved in that format
im = Image.open(TEST_GIF)
im = Image.open("Tests/images/test.colors.gif")
im.save(out)
reread = Image.open(out)
self.assertEqual(reread.info["version"], b"GIF87a")