Clear TIFF core image if memory mapping was used for last load by radarhere · Pull Request #8962 · python-pillow/Pillow

Resolves #8961

I've used the test image from the issue, but due to a larger file size, I've placed it in python-pillow/test-images#7. This will fail without that pull request.

Using memory mapping to load one frame, and then not using it for the next frame, is causing a problem. This would be because memory mapping is replacing the core image.

self.im = Image.core.map_buffer(
self.map, self.size, decoder_name, offset, args
)

But TiffImagePlugin tries to keep using it afterwards.

def seek(self, frame: int) -> None:
"""Select a given frame as current image"""
if not self._seek_check(frame):
return
self._seek(frame)
if self._im is not None and (
self.im.size != self._tile_size or self.im.mode != self.mode
):
# The core image will no longer be used
self._im = None