Skip to content

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented May 21, 2025

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.

Pillow/src/PIL/ImageFile.py

Lines 324 to 326 in 3c71559

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

@wiredfool
Copy link
Member

Does it make sense to set the c level read-only flag on the image if it's memmapped, and then check if the image is read-only instead? Would the mmap also cause an issue if someone were to try to imagedraw on it?

@radarhere
Copy link
Member Author

I've updated the commit to use the Python readonly flag.

ImageDraw should be fine, thanks to this code in ImageDraw.__init__.

if im.readonly:
im._copy() # make it writeable

wiredfool and others added 2 commits May 26, 2025 08:15
Mark the image read-only in the C layer if it's created from a read only buffer
@radarhere radarhere merged commit b844007 into python-pillow:main Jun 10, 2025
68 of 97 checks passed
@radarhere radarhere deleted the tiff_mmap branch June 10, 2025 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault when seeking to position 0 from any position in a TIFF image and reading the numpy array

2 participants