diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
index 2ea0f106e..34c6f39de 100644
--- a/src/PIL/TiffImagePlugin.py
+++ b/src/PIL/TiffImagePlugin.py
@@ -1056,7 +1056,6 @@ class TiffImageFile(ImageFile.ImageFile):
             self.__frame += 1
         self.fp.seek(self._frame_pos[frame])
         self.tag_v2.load(self.fp)
-        self.__next = self.tag_v2.next
         # fill the legacy tag/ifd entries
         self.tag = self.ifd = ImageFileDirectory_v1.from_v2(self.tag_v2)
         self.__frame = frame
@@ -1087,7 +1086,7 @@ class TiffImageFile(ImageFile.ImageFile):
     def load_end(self):
         # allow closing if we're on the first frame, there's no next
         # This is the ImageFile.load path only, libtiff specific below.
-        if self.__frame == 0 and not self.__next:
+        if len(self._frame_pos) == 1 and not self.__next:
             self._close_exclusive_fp_after_loading = True
 
     def _load_libtiff(self):
@@ -1168,7 +1167,7 @@ class TiffImageFile(ImageFile.ImageFile):
         self.readonly = 0
         # libtiff closed the fp in a, we need to close self.fp, if possible
         if self._exclusive_fp:
-            if self.__frame == 0 and not self.__next:
+            if len(self._frame_pos) == 1 and not self.__next:
                 self.fp.close()
                 self.fp = None  # might be shared
 

Reading multi-image TIFF gets wrong frame after seeking backwards
Opening a multi-image tiff file and seeking backwards and then forwards can seek to the wrong frame.  Below is a test case that demonstrates the bug, and a patch that fixes it.

The attached zip file contains tiff_seek_test.py that writes a 3 frame TIFF file and demonstrates that seeking to frames 0, 1, 0, 2 in that order ends with incorrectly being on frame 1 instead of frame 2.  The test sets the pixel values of each frame to match the frame number and reports the pixel values of each frame.

[tiff_seek.zip](https://github.com/python-pillow/Pillow/files/2958894/tiff_seek.zip)

Test was run with Python 3.7 and PIL 5.4.1 on macOS 10.14.2.  PIL 6.0 code has the same faulty TiffImagePlugin.py _seek() code.  The above zip file includes a patch that fixes this bug.

The TiffImagePlugin.py _seek() code incorrectly assumes that the current frame is the last frame in its list of cached frame byte offsets (TiffImageFile._frame_pos).  That assumption is correct if only forward seeking is done, after a backward seek the current frame is not the last one in the cached frame byte offset list, and this results in caching the wrong byte offset after a seek to a frame not in the cached byte offsets.

diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py
index 9a4104b78..34911ed50 100644
--- a/Tests/test_file_tiff.py
+++ b/Tests/test_file_tiff.py
@@ -263,6 +263,11 @@ class TestFileTiff(PillowTestCase):
         self.assertEqual(im.size, (10, 10))
         self.assertEqual(im.convert('RGB').getpixel((0, 0)), (255, 0, 0))
 
+        im.seek(0)
+        im.load()
+        self.assertEqual(im.size, (10, 10))
+        self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 128, 0))
+
         im.seek(2)
         im.load()
         self.assertEqual(im.size, (20, 20))

pip install -e .[dev]
parse_log_pytest
requirements.txt
apt-get update apt-get install -y gcc libjpeg-dev zlib1g-dev libtiff5-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev libharfbuzz-dev libfribidi-dev libxcb1-dev
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alabaster==0.7.16
babel==2.17.0
blessed==1.20.0
build==1.2.2.post1
certifi==2025.1.31
charset-normalizer==3.4.1
check-manifest==0.50
cov-core==1.15.0
coverage==7.8.0
coveralls==4.0.1
docopt==0.6.2
docutils==0.21.2
exceptiongroup==1.2.2
idna==3.10
imagesize==1.4.1
importlib_metadata==8.6.1
iniconfig==2.1.0
jarn.viewdoc==2.7
Jinja2==3.1.6
MarkupSafe==3.0.2
olefile==0.47
packaging==24.2
-e git+https://github.com/python-pillow/Pillow.git@3e9457a034736cd4e320cfcddbc2de7cd4f4d7e5#egg=Pillow
pluggy==1.5.0
pycodestyle==2.13.0
pyflakes==3.3.1
Pygments==2.19.1
pyproject_hooks==1.2.0
pyroma==4.2
pytest==8.3.5
pytest-cov==6.0.0
pytz==2025.2
requests==2.32.3
six==1.17.0
snowballstemmer==2.2.0
Sphinx==7.4.7
sphinx-rtd-theme==3.0.2
sphinxcontrib-applehelp==2.0.0
sphinxcontrib-devhelp==2.0.0
sphinxcontrib-htmlhelp==2.1.0
sphinxcontrib-jquery==4.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==2.0.0
sphinxcontrib-serializinghtml==2.0.0
tomli==2.2.1
trove-classifiers==2025.3.19.19
urllib3==2.3.0
wcwidth==0.2.13
zipp==3.21.0

name: Pillow
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - alabaster==0.7.16
      - babel==2.17.0
      - blessed==1.20.0
      - build==1.2.2.post1
      - certifi==2025.1.31
      - charset-normalizer==3.4.1
      - check-manifest==0.50
      - cov-core==1.15.0
      - coverage==7.8.0
      - coveralls==4.0.1
      - docopt==0.6.2
      - docutils==0.21.2
      - exceptiongroup==1.2.2
      - idna==3.10
      - imagesize==1.4.1
      - importlib-metadata==8.6.1
      - iniconfig==2.1.0
      - jarn-viewdoc==2.7
      - jinja2==3.1.6
      - markupsafe==3.0.2
      - olefile==0.47
      - packaging==24.2
      - pluggy==1.5.0
      - pycodestyle==2.13.0
      - pyflakes==3.3.1
      - pygments==2.19.1
      - pyproject-hooks==1.2.0
      - pyroma==4.2
      - pytest==8.3.5
      - pytest-cov==6.0.0
      - pytz==2025.2
      - requests==2.32.3
      - six==1.17.0
      - snowballstemmer==2.2.0
      - sphinx==7.4.7
      - sphinx-rtd-theme==3.0.2
      - sphinxcontrib-applehelp==2.0.0
      - sphinxcontrib-devhelp==2.0.0
      - sphinxcontrib-htmlhelp==2.1.0
      - sphinxcontrib-jquery==4.1
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==2.0.0
      - sphinxcontrib-serializinghtml==2.0.0
      - tomli==2.2.1
      - trove-classifiers==2025.3.19.19
      - urllib3==2.3.0
      - wcwidth==0.2.13
      - zipp==3.21.0
prefix: /opt/conda/envs/Pillow

Tests/test_file_tiff.py::TestFileTiff::test_multipage
Tests/test_file_tiff.py::TestFileTiff::test_unclosed_file
Tests/test_file_tiff.py::TestFileTiff::test_12bit_rawmode Tests/test_file_tiff.py::TestFileTiff::test_16bit_s Tests/test_file_tiff.py::TestFileTiff::test_32bit_float Tests/test_file_tiff.py::TestFileTiff::test_4bit Tests/test_file_tiff.py::TestFileTiff::test___str__ Tests/test_file_tiff.py::TestFileTiff::test__delitem__ Tests/test_file_tiff.py::TestFileTiff::test__limit_rational_float Tests/test_file_tiff.py::TestFileTiff::test__limit_rational_int Tests/test_file_tiff.py::TestFileTiff::test_bad_exif Tests/test_file_tiff.py::TestFileTiff::test_big_endian Tests/test_file_tiff.py::TestFileTiff::test_close_on_load_exclusive Tests/test_file_tiff.py::TestFileTiff::test_close_on_load_nonexclusive Tests/test_file_tiff.py::TestFileTiff::test_dict Tests/test_file_tiff.py::TestFileTiff::test_eoferror Tests/test_file_tiff.py::TestFileTiff::test_gray_semibyte_per_pixel Tests/test_file_tiff.py::TestFileTiff::test_int_resolution Tests/test_file_tiff.py::TestFileTiff::test_invalid_file Tests/test_file_tiff.py::TestFileTiff::test_little_endian Tests/test_file_tiff.py::TestFileTiff::test_load_byte Tests/test_file_tiff.py::TestFileTiff::test_load_double Tests/test_file_tiff.py::TestFileTiff::test_load_float Tests/test_file_tiff.py::TestFileTiff::test_load_string Tests/test_file_tiff.py::TestFileTiff::test_mac_tiff Tests/test_file_tiff.py::TestFileTiff::test_multipage_last_frame Tests/test_file_tiff.py::TestFileTiff::test_n_frames Tests/test_file_tiff.py::TestFileTiff::test_roundtrip_tiff_uint16 Tests/test_file_tiff.py::TestFileTiff::test_sanity Tests/test_file_tiff.py::TestFileTiff::test_save_rgba Tests/test_file_tiff.py::TestFileTiff::test_save_setting_missing_resolution Tests/test_file_tiff.py::TestFileTiff::test_save_unsupported_mode Tests/test_file_tiff.py::TestFileTiff::test_saving_icc_profile Tests/test_file_tiff.py::TestFileTiff::test_seek Tests/test_file_tiff.py::TestFileTiff::test_seek_eof Tests/test_file_tiff.py::TestFileTiff::test_set_legacy_api Tests/test_file_tiff.py::TestFileTiff::test_size Tests/test_file_tiff.py::TestFileTiff::test_strip_planar_raw Tests/test_file_tiff.py::TestFileTiff::test_strip_planar_raw_with_overviews Tests/test_file_tiff.py::TestFileTiff::test_strip_raw Tests/test_file_tiff.py::TestFileTiff::test_tiff_save_all Tests/test_file_tiff.py::TestFileTiff::test_tiled_planar_raw Tests/test_file_tiff.py::TestFileTiff::test_unknown_pixel_mode Tests/test_file_tiff.py::TestFileTiff::test_with_underscores Tests/test_file_tiff.py::TestFileTiff::test_wrong_bits_per_sample Tests/test_file_tiff.py::TestFileTiff::test_xyres_fallback_tiff Tests/test_file_tiff.py::TestFileTiff::test_xyres_tiff
src/PIL/TiffImagePlugin.py
src/PIL/TiffImagePlugin.py