diff --git a/Tests/images/multiple_comments.gif b/Tests/images/multiple_comments.gif new file mode 100644 index 000000000..88b2af800 Binary files /dev/null and b/Tests/images/multiple_comments.gif differ diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 8f91177eb..c2cf5e464 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -831,6 +831,12 @@ def test_zero_comment_subblocks(): assert_image_equal_tofile(im, TEST_GIF) +def test_read_multiple_comment_blocks(): + with Image.open("Tests/images/multiple_comments.gif") as im: + # Multiple comment blocks in a frame are separated not concatenated + assert im.info["comment"] == b"Test comment 1\nTest comment 2" + + def test_version(tmp_path): out = str(tmp_path / "temp.gif") diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 8b03680a8..403f24215 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -228,12 +228,18 @@ class GifImageFile(ImageFile.ImageFile): # # comment extension # + comment = b"" + + # Collect one comment block while block: - if "comment" in info: - info["comment"] += block - else: - info["comment"] = block + comment += block block = self.data() + + if "comment" in info: + # If multiple comment blocks in frame, separate with \n + info["comment"] += b"\n" + comment + else: + info["comment"] = comment s = None continue elif s[0] == 255: