From e8bb2dd93ea7ca600e48589e4118ecea2426afbc Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 1 Jan 2020 13:29:39 +1100 Subject: [PATCH] Allow string comment when saving GIF --- Tests/test_file_gif.py | 14 +++++++++----- src/PIL/GifImagePlugin.py | 7 +++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index bbd589ada..61c3d8f78 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -565,14 +565,18 @@ class TestFileGif(PillowTestCase): 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) + out = self.tempfile("temp.gif") + im = Image.new("L", (100, 100), "#000") + 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") diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 5f9ba59c1..63a0f662b 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -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: