Allow string comment when saving GIF

This commit is contained in:
Andrew Murray 2020-01-01 13:29:39 +11:00
parent 94ec95c571
commit e8bb2dd93e
2 changed files with 14 additions and 7 deletions

View File

@ -570,9 +570,13 @@ class TestFileGif(PillowTestCase):
im.info["comment"] = b"Test comment text"
im.save(out)
with Image.open(out) as reread:
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):
out = self.tempfile("temp.gif")
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"]):
fp.write(b"!" + o8(254)) # extension intro
for i in range(0, len(im.encoderinfo["comment"]), 255):
subblock = im.encoderinfo["comment"][i : i + 255]
comment = im.encoderinfo["comment"]
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(0))
if "loop" in im.encoderinfo: