New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading large TIFF fails with OSError: -9 #5370
Comments
Did you confirm this, e.g. by running
Don't think one would need to go to fine-grained control like that (although ImageMagick does expose this for example), but instead always use either libtiff's default strip size calculation (which will aim for strips of about 8KB for default libtiff builds, or one row at least), or pick another suitable strip size target (tifffile goes for 64KB for example). |
I looked at the tiff tags with Pillow. And found at this line of code from TiffImagePlugin.py that sets RowsPerStrip to the height of the image. Confirming again with exiftool (only relevant section shown):
And for comparison the image saved again with Preview.app:
Agreed. |
The TIFF spec is also pretty clear on this:
|
Hello, I have also hit this bug with this much smaller image: IMSLP00022-001.zip $ exiftool -a -u -s -g1 IMSLP00022-001.tiff
---- ExifTool ----
ExifToolVersion : 12.16
---- System ----
FileName : IMSLP00022-001.tiff
Directory : .
FileSize : 100 KiB
FileModifyDate : 2020:08:12 18:04:14+02:00
FileAccessDate : 2021:04:19 11:45:22+02:00
FileInodeChangeDate : 2021:04:12 12:06:05+02:00
FilePermissions : rw-r--r--
---- File ----
FileType : TIFF
FileTypeExtension : tif
MIMEType : image/tiff
ExifByteOrder : Little-endian (Intel, II)
---- IFD0 ----
ImageWidth : 2501
ImageHeight : 3554
BitsPerSample : 1
Compression : T6/Group 4 Fax
PhotometricInterpretation : WhiteIsZero
FillOrder : Normal
StripOffsets : 8
Orientation : Horizontal (normal)
SamplesPerPixel : 1
RowsPerStrip : 4294967295
StripByteCounts : 102393
XResolution : 204
YResolution : 196
PlanarConfiguration : Chunky
T6Options : (none)
ResolutionUnit : inches
PageNumber : 0 1
Software : fax2tiff
---- Composite ----
ImageSize : 2501x3554
Megapixels : 8.9 Let me know if you need more information. |
@kwon-young I find that I'm not able to open your image with Pillow 8.1.2, but I am with Pillow 8.2.0. So if you update, I expect your problem to be fixed. |
Ah thanks, I'm using conda-forge where pillow is still on 8.1.2. |
It seems the original problem with large images remains in 8.2.0:
|
Apart from WIP trying to limit the PIL writing side, the reading should ideally be spec compliant and support 4 GB max image size. As libtiff internal implementation is limited to int32_t counters, they get around this by the following re-jigging of the directory, so maybe this should be leveraged somehow in Looks like adding a |
The test code above now passes with #5514 as the compressed file is also now saved w/
|
So the original problem is now fixed. It sounds like you'd like this issue to stay open for the sake of the more general problem though @kmilos. Could you provide sample failing code to demonstrate the problem? |
I'm ok with this being closed. There is #5517 outstanding that is somewhat related, but there is no way to test it AFAICT... |
Thanks |
What did you do?
Load a large TIFF created previously with Pillow.
What did you expect to happen?
Load without error.
What actually happened?
What are your OS, Python and Pillow versions?
Code:
I believe we are hitting the size limit limit here, because reducing
height
to35_791
in the above code makes it work.30.000 x 35.792 x 2 bytes = 2.147.520.000 bytes -> error -9 (IMAGING_CODEC_MEMORY)
30.000 x 35.791 x 2 bytes = 2.147.460.000 bytes -> works
This leads me to believe that INT_MAX is 2.147.483.647, so it's an
int32
(32 bit, signed), where it could be 64 bit (on supported platforms) and unsigned to maximize the size of loadable images.In addition I noticed the same image CAN be loaded by pillow when re-saved in Preview.app (or Affinity Photo for that matter). It looks like Preview.app saves it in strips of a single row each, which adds some metadata but gets around the memory limit. Pillow seems to save a single strip containing all rows. So it would be cool to be able to control layout of strips when saving tiffs, as it would allow to get around the memory problem as well.
Attachment: Example TIFF + TIFF saved with Preview.app
The text was updated successfully, but these errors were encountered: