Skip to content

Commit e9cb215

Browse files
authored
Merge pull request #6294 from raygard/comments_separate_multiple
Separate multiple GIF comment blocks in a frame with newlines
2 parents 22d797f + 62d5817 commit e9cb215

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Tests/images/multiple_comments.gif

1.5 KB
Loading

Tests/test_file_gif.py

+6
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,12 @@ def test_zero_comment_subblocks():
831831
assert_image_equal_tofile(im, TEST_GIF)
832832

833833

834+
def test_read_multiple_comment_blocks():
835+
with Image.open("Tests/images/multiple_comments.gif") as im:
836+
# Multiple comment blocks in a frame are separated not concatenated
837+
assert im.info["comment"] == b"Test comment 1\nTest comment 2"
838+
839+
834840
def test_version(tmp_path):
835841
out = str(tmp_path / "temp.gif")
836842

src/PIL/GifImagePlugin.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,18 @@ def _seek(self, frame, update_image=True):
228228
#
229229
# comment extension
230230
#
231+
comment = b""
232+
233+
# Collect one comment block
231234
while block:
232-
if "comment" in info:
233-
info["comment"] += block
234-
else:
235-
info["comment"] = block
235+
comment += block
236236
block = self.data()
237+
238+
if "comment" in info:
239+
# If multiple comment blocks in frame, separate with \n
240+
info["comment"] += b"\n" + comment
241+
else:
242+
info["comment"] = comment
237243
s = None
238244
continue
239245
elif s[0] == 255:

0 commit comments

Comments
 (0)