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 3c2fab722..9fb171411 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -813,6 +813,12 @@ def test_zero_comment_subblocks(): assert_image_equal_tofile(im, TEST_GIF) +def test_read_multiple_comments(): + with Image.open("Tests/images/multiple_comments.gif") as im: + # Multiple comments in a frame are separated not concatenated + assert im.info["comment"] == b"Test comment 1\r\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 9b34a3b0e..6c2b1dedf 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -228,12 +228,17 @@ class GifImageFile(ImageFile.ImageFile): # # comment extension # + # Collect one comment block + comment = b"" while block: - if "comment" in info: - info["comment"] += block - else: - info["comment"] = block + comment += block block = self.data() + + # If multiple comments in frame, separate in info with \r\n + if "comment" in info: + info["comment"] += b"\r\n" + comment + else: + info["comment"] = comment s = None continue elif s[0] == 255: