Repository: python-pillow/Pillow

Issue:
BUG: n_frames is incorrect
<!--
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?
i try to load every frame of a gif by  the following code

```python
# PILLOW==9.1.0

img = Image.open('./test.gif')
for I in range(img.n_frames):
    tmp.seek(i)
```

below is the test img and I find that `img.n_frames` gives me 2 but the actual frame number is 1,so the seek function raise an EOF error. 

![13077030995698418847](https://user-images.githubusercontent.com/22706545/169772374-2a911010-4562-4fcd-8c5a-47b0642a111e.gif)



### What did you expect to happen?
load all the frame without any error

### What actually happened?

The following error is raised.
raise EOFError("no more images in GIF file") from e


### What are your OS, Python and Pillow versions?


* OS: macos m1
* Python: 3.7.0
* 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.
-->

```python
from PIL import Image
img = Image.open('./test.gif')
for I in range(img.n_frames):
    tmp.seek(i)
```



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
@@ -183,8 +183,6 @@ def _seek(self, frame, update_image=True):
         if not s or s == b";":
             raise EOFError
 
-        self.__frame = frame
-
         self.tile = []
 
         palette = None
@@ -283,6 +281,8 @@ def _seek(self, frame, update_image=True):
         if interlace is None:
             # self.__fp = None
             raise EOFError
+
+        self.__frame = frame
         if not update_image:
             return
 


Test patch:
diff --git a/Tests/images/comment_after_last_frame.gif b/Tests/images/comment_after_last_frame.gif
new file mode 100644
Binary files /dev/null and b/Tests/images/comment_after_last_frame.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
@@ -341,16 +341,23 @@ def test_seek_rewind():
             assert_image_equal(im, expected)
 
 
-def test_n_frames():
-    for path, n_frames in [[TEST_GIF, 1], ["Tests/images/iss634.gif", 42]]:
-        # Test is_animated before n_frames
-        with Image.open(path) as im:
-            assert im.is_animated == (n_frames != 1)
-
-        # Test is_animated after n_frames
-        with Image.open(path) as im:
-            assert im.n_frames == n_frames
-            assert im.is_animated == (n_frames != 1)
+@pytest.mark.parametrize(
+    "path, n_frames",
+    (
+        (TEST_GIF, 1),
+        ("Tests/images/comment_after_last_frame.gif", 2),
+        ("Tests/images/iss634.gif", 42),
+    ),
+)
+def test_n_frames(path, n_frames):
+    # Test is_animated before n_frames
+    with Image.open(path) as im:
+        assert im.is_animated == (n_frames != 1)
+
+    # Test is_animated after n_frames
+    with Image.open(path) as im:
+        assert im.n_frames == n_frames
+        assert im.is_animated == (n_frames != 1)
 
 
 def test_no_change():