mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-05 13:43:28 +03:00
Merge pull request #1896 from radarhere/comment
Added support for GIF comment extension
This commit is contained in:
commit
1eedd2ef15
|
@ -198,6 +198,11 @@ class GifImageFile(ImageFile.ImageFile):
|
||||||
# correct, but it seems to prevent the last
|
# correct, but it seems to prevent the last
|
||||||
# frame from looking odd for some animations
|
# frame from looking odd for some animations
|
||||||
self.disposal_method = dispose_bits
|
self.disposal_method = dispose_bits
|
||||||
|
elif i8(s) == 254:
|
||||||
|
#
|
||||||
|
# comment extension
|
||||||
|
#
|
||||||
|
self.info["comment"] = block
|
||||||
elif i8(s) == 255:
|
elif i8(s) == 255:
|
||||||
#
|
#
|
||||||
# application extension
|
# application extension
|
||||||
|
@ -455,6 +460,12 @@ def _get_local_header(fp, im, offset, flags):
|
||||||
o8(transparency) + # transparency index
|
o8(transparency) + # transparency index
|
||||||
o8(0))
|
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:
|
if "loop" in im.encoderinfo:
|
||||||
number_of_loops = im.encoderinfo["loop"]
|
number_of_loops = im.encoderinfo["loop"]
|
||||||
fp.write(b"!" +
|
fp.write(b"!" +
|
||||||
|
@ -547,9 +558,11 @@ def getheader(im, palette=None, info=None):
|
||||||
# http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
|
# http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
|
||||||
|
|
||||||
version = b"87a"
|
version = b"87a"
|
||||||
for extensionKey in ["transparency", "duration", "loop"]:
|
for extensionKey in ["transparency", "duration", "loop", "comment"]:
|
||||||
if info and extensionKey in info and \
|
if info and extensionKey in info:
|
||||||
not (extensionKey == "duration" and info[extensionKey] == 0):
|
if ((extensionKey == "duration" and info[extensionKey] == 0) or
|
||||||
|
(extensionKey == "comment" and not (1 <= len(info[extensionKey]) <= 255))):
|
||||||
|
continue
|
||||||
version = b"89a"
|
version = b"89a"
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -267,6 +267,18 @@ class TestFileGif(PillowTestCase):
|
||||||
|
|
||||||
self.assertEqual(reread.info['background'], im.info['background'])
|
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):
|
def test_version(self):
|
||||||
out = self.tempfile('temp.gif')
|
out = self.tempfile('temp.gif')
|
||||||
|
|
||||||
|
@ -283,7 +295,7 @@ class TestFileGif(PillowTestCase):
|
||||||
self.assertEqual(reread.info["version"], b"GIF89a")
|
self.assertEqual(reread.info["version"], b"GIF89a")
|
||||||
|
|
||||||
# Test that a GIF87a image is also saved in that format
|
# 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)
|
im.save(out)
|
||||||
reread = Image.open(out)
|
reread = Image.open(out)
|
||||||
self.assertEqual(reread.info["version"], b"GIF87a")
|
self.assertEqual(reread.info["version"], b"GIF87a")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user