Repository: python-pillow/Pillow

Issue:
GIF: multiple comment blocks for a frame are run together
<!--
Thank you for reporting an issue.

Follow these guidelines to ensure your issue is handled properly.

If you have a ...

1.  General question: consider asking the question on Stack Overflow
    with the python-imaging-library tag:

    * https://stackoverflow.com/questions/tagged/python-imaging-library

    Do not ask a question in both places.

    If you think you have found a bug or have an unexplained exception
    then file a bug report here.

2.  Bug report: include a self-contained, copy-pastable example that
    generates the issue if possible. Be concise with code posted.
    Guidelines on how to provide a good bug report:

    * https://stackoverflow.com/help/mcve

    Bug reports which follow these guidelines are easier to diagnose,
    and are often handled much more quickly.

3.  Feature request: do a quick search of existing issues
    to make sure this has not been asked before.

We know asking good questions takes effort, and we appreciate your time.
Thank you.
-->

### What did you do?
Read a GIF file with two or more comments in a single frame

### What did you expect to happen?
They would somehow be kept separate in the `.info` dict

### What actually happened?
The comments are run together, e.g. separate comments `foo` and `bar` become `foobar`

### What are your OS, Python and Pillow versions?

* OS: Windows 10
* Python: 3.10.4
* Pillow: 9.1.0

<!--
Please include **code** that reproduces the issue and whenever possible, an **image** that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as Plone, Django, or Buildout, try to replicate the issue just using Pillow.
-->

Example image:

![multiple_comments](https://user-images.githubusercontent.com/24783736/168400700-03d5a7a8-5329-4bde-80b9-b6d58a2a033b.gif)

```python
from PIL import Image
with Image.open("multiple_comments.gif") as im:
    print(f'{im.info["comment"]=}')
```
prints 
```
im.info["comment"]=b'Test comment 1Test comment 2'
```
I made PR #6294 to address this, that will separate the comments with `\r\n`.

I admit that such cases are rare, but I have a few found in the wild. Unfortunately most of them have the multiple comments after the last image block, and at present I see no way in Pillow to access comments beyond the last image block. I do have one with 10 comment blocks before the image, a logo from my former employer:

![b6483cc24863f387173dd6fb5a150ede_image003](https://user-images.githubusercontent.com/24783736/168401052-648a8862-82d0-4846-a348-4fc51e617834.gif)


Discussion:

Resolving patch:
diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py
--- a/src/PIL/GifImagePlugin.py
+++ b/src/PIL/GifImagePlugin.py
@@ -228,12 +228,18 @@ def _seek(self, frame, update_image=True):
                     #
                     # 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:


Test patch:
diff --git a/Tests/images/multiple_comments.gif b/Tests/images/multiple_comments.gif
new file mode 100644
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
--- 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_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")