Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for GIF comment extension #1896

Merged
merged 1 commit into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ def _seek(self, frame):
# 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
Expand Down Expand Up @@ -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"!" +
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 13 additions & 1 deletion Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ def test_background(self):

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

Expand All @@ -283,7 +295,7 @@ def test_version(self):
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")
Expand Down