Merge pull request #4326 from radarhere/comment

Allow string comment when saving GIF
This commit is contained in:
Hugo van Kemenade 2020-01-01 06:19:12 +02:00 committed by GitHub
commit bca14ebf1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -565,14 +565,18 @@ class TestFileGif(PillowTestCase):
im.info["comment"], b"File written by Adobe Photoshop\xa8 4.0" im.info["comment"], b"File written by Adobe Photoshop\xa8 4.0"
) )
out = self.tempfile("temp.gif") out = self.tempfile("temp.gif")
im = Image.new("L", (100, 100), "#000") im = Image.new("L", (100, 100), "#000")
im.info["comment"] = b"Test comment text" im.info["comment"] = b"Test comment text"
im.save(out) im.save(out)
with Image.open(out) as reread: with Image.open(out) as reread:
self.assertEqual(reread.info["comment"], im.info["comment"]) self.assertEqual(reread.info["comment"], im.info["comment"])
im.info["comment"] = "Test comment text"
im.save(out)
with Image.open(out) as reread:
self.assertEqual(reread.info["comment"], im.info["comment"].encode())
def test_comment_over_255(self): def test_comment_over_255(self):
out = self.tempfile("temp.gif") out = self.tempfile("temp.gif")
im = Image.new("L", (100, 100), "#000") im = Image.new("L", (100, 100), "#000")

View File

@ -569,8 +569,11 @@ def _write_local_header(fp, im, offset, flags):
if "comment" in im.encoderinfo and 1 <= len(im.encoderinfo["comment"]): if "comment" in im.encoderinfo and 1 <= len(im.encoderinfo["comment"]):
fp.write(b"!" + o8(254)) # extension intro fp.write(b"!" + o8(254)) # extension intro
for i in range(0, len(im.encoderinfo["comment"]), 255): comment = im.encoderinfo["comment"]
subblock = im.encoderinfo["comment"][i : i + 255] if isinstance(comment, str):
comment = comment.encode()
for i in range(0, len(comment), 255):
subblock = comment[i : i + 255]
fp.write(o8(len(subblock)) + subblock) fp.write(o8(len(subblock)) + subblock)
fp.write(o8(0)) fp.write(o8(0))
if "loop" in im.encoderinfo: if "loop" in im.encoderinfo: