python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.27k stars 2.23k forks source link

Multi-page tiff with a large amount of pages #3919

Closed dsame89 closed 5 years ago

dsame89 commented 5 years ago

Hi,

I have a problem with multi-page tiff with over a 150 pages. Unable to do any operations on the pages over 150.

Image: 3 GB 2480x3508 300 dpi Color dept: 24 bit Compression LZW 222 pages in total

Code:

temp = Image.open("image.tiff") 
temp.seek(160)
temp.save("image2.tif")

Stacktrace:

Traceback (most recent call last):
  File "testarar.py", line 72, in <module>
    temp.save("image.tif")
  File "X/PIL/Image.py", line 1973, in save
    self._ensure_mutable()
  File "X/PIL/Image.py", line 628, in _ensure_mutable
    self._copy()
  File "X/PIL/Image.py", line 621, in _copy
    self.load()
  File "X/PIL/TiffImagePlugin.py", line 1077, in load
    return self._load_libtiff()
  File "X/PIL/TiffImagePlugin.py", line 1127, in _load_libtiff
    self.decoderconfig)
  File "X/PIL/Image.py", line 455, in _getdecoder
    return decoder(mode, *args + extra)
OverflowError: signed integer is greater than maximum
cgohlke commented 5 years ago

I was able to reproduce this issue with a synthetic deflate compressed multi-page file.

PR #3923 should fix the OverflowError.

Another issue is that the file produced by Pillow is IEEEFP8, while the original image is UINT8, e.g.

TiffFile 'temp2.tif'  16.47 MiB

TiffPageSeries 0  2480x3508x3  float64  YXS  Generic  1 Pages

TiffPage 0 @17272310  2480x3508x3  ieeefp8  rgb adobe_deflate

TiffTag 254 NewSubfileType 1I @17272320  0
TiffTag 256 ImageWidth 1H @17272332  3508
TiffTag 257 ImageLength 1H @17272344  2480
TiffTag 258 BitsPerSample 3H @17272524  (8, 8, 8)
TiffTag 259 Compression 1H @17272368  ADOBE_DEFLATE
TiffTag 262 PhotometricInterpretation 1H @17272380  RGB
TiffTag 273 StripOffsets 1I @17272392  (8,)
TiffTag 277 SamplesPerPixel 1H @17272404  3
TiffTag 278 RowsPerStrip 1H @17272416  2480
TiffTag 279 StripByteCounts 1I @17272428  (17272301,)
TiffTag 282 XResolution 2I @17272508  (1, 1)
TiffTag 283 YResolution 2I @17272516  (1, 1)
TiffTag 284 PlanarConfiguration 1H @17272464  CONTIG
TiffTag 296 ResolutionUnit 1H @17272476  NONE
TiffTag 305 Software 12s @17272536  tifffile.py
TiffTag 339 SampleFormat 3H @17272530  ('IEEEFP', 'IEEEFP', 'IEEEFP')
TiffFile 'pillow_issue_3919.tif'  3.21 GiB   200 Pages

TiffPageSeries 0  200x2480x3508x3  uint8  IYXS  Uniform  200 Pages

TiffPage 0 @8  2480x3508x3  uint8  rgb adobe_deflate

TiffTag 254 NewSubfileType 1I @18  0
TiffTag 256 ImageWidth 1I @30  3508
TiffTag 257 ImageLength 1I @42  2480
TiffTag 258 BitsPerSample 3H @206  (8, 8, 8)
TiffTag 259 Compression 1H @66  ADOBE_DEFLATE
TiffTag 262 PhotometricInterpretation 1H @78  RGB
TiffTag 273 StripOffsets 414I @212  (3568, 45209, 86849, 128496, 170149, 211820
TiffTag 277 SamplesPerPixel 1H @102  3
TiffTag 278 RowsPerStrip 1I @114  6
TiffTag 279 StripByteCounts 414I @1868  (41641, 41640, 41647, 41653, 41671, 416
TiffTag 282 XResolution 2I @3524  (1, 1)
TiffTag 283 YResolution 2I @3532  (1, 1)
TiffTag 284 PlanarConfiguration 1H @162  CONTIG
TiffTag 296 ResolutionUnit 1H @174  NONE
TiffTag 305 Software 12s @3540  tifffile.py
TiffTag 339 SampleFormat 3H @3552  ('UINT', 'UINT', 'UINT')