From 886a7cf206cacd41091b2de51045fe252bbe30f6 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 27 Mar 2016 22:18:39 +1100 Subject: [PATCH 1/2] Different TIFF frames may have different compression --- PIL/TiffImagePlugin.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 096be6f56..cc739aec6 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -238,8 +238,8 @@ class IFDRational(Rational): as a fractions.Fraction(). Delegate as appropriate """ - - __slots__ = ('_numerator', '_denominator', '_val') + + __slots__ = ('_numerator', '_denominator', '_val') def __init__(self, value, denominator=1): """ @@ -255,7 +255,7 @@ class IFDRational(Rational): self._numerator = value.numerator self._denominator = value.denominator self._val = value - + if type(value) == IFDRational: self._denominator = value.denominator self._numerator = value.numerator @@ -287,7 +287,7 @@ class IFDRational(Rational): def limit_rational(self, max_denominator): """ - + :param max_denominator: Integer, the maximum denominator value :returns: Tuple of (numerator, denominator) """ @@ -349,7 +349,7 @@ class IFDRational(Rational): __floor__ = _delegate('__floor__') __round__ = _delegate('__round__') - + class ImageFileDirectory_v2(collections.MutableMapping): """This class represents a TIFF tag directory. To speed things up, we @@ -991,6 +991,11 @@ class TiffImageFile(ImageFile.ImageFile): return args + def load(self): + if self.use_load_libtiff: + return self._load_libtiff() + return super(TiffImageFile, self).load() + def _load_libtiff(self): """ Overload method triggered when we detect a compressed tiff Calls out to libtiff """ @@ -1136,6 +1141,7 @@ class TiffImageFile(ImageFile.ImageFile): # build tile descriptors x = y = l = 0 self.tile = [] + self.use_load_libtiff = False if STRIPOFFSETS in self.tag_v2: # striped image offsets = self.tag_v2[STRIPOFFSETS] @@ -1158,10 +1164,9 @@ class TiffImageFile(ImageFile.ImageFile): # function. # # Setup the one tile for the whole image, then - # replace the existing load function with our - # _load_libtiff function. + # use the _load_libtiff function. - self.load = self._load_libtiff + self.use_load_libtiff = True # To be nice on memory footprint, if there's a # file descriptor, use that instead of reading From 33fff9e4a89d6242bf67f6a677860d4af05c172a Mon Sep 17 00:00:00 2001 From: Geka Date: Mon, 29 Feb 2016 15:07:51 +0300 Subject: [PATCH 2/2] Add test for multipage tiff file with different compression --- Tests/images/compression.tif | Bin 0 -> 646 bytes Tests/test_file_tiff.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+) create mode 100755 Tests/images/compression.tif diff --git a/Tests/images/compression.tif b/Tests/images/compression.tif new file mode 100755 index 0000000000000000000000000000000000000000..2488b7841c5461da9c661fc7c32abc8a37f71e5f GIT binary patch literal 646 zcmebD)MAifU|=u}k@YEk(0X8N!_kDN3|}71l3B#S$MBDVg@F+$%LK#>jLcv*7l?_( zW`eRo>X@Nyka{*K8^~l3WQ3T*0c49JsTYH?L1v0W)d&OGQjDx%yG{Yw(ops#AX^4W zuOTB7g9A|P77%v;El&Z`AUDA15KlKZpUfnMGDAHx1BIg0GKECLq$JBUQ*9#yLo;0i zBV8j)1w#`n6B8=~BRvI;WKD&j5CtOx12bKa0tQY7y`Y@xhU%OkJx;xthM6nZt(@5q z!x_}DWbc_vXZ9{>;LMr1_xiKf&#v#C$yvSf%(Jh*zCJs%lCxpmrPsgy|9X9C9kM8r zS|k$?=8_iHpm2nRHERPRtU>;Vg|!rrLny5Cfa+mkZ3c`qSXkQu+1SF`4HU6J01IOn IO=(yI0Na&zcK`qY literal 0 HcmV?d00001 diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 20a0e93e3..0fc8a5bad 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -378,6 +378,22 @@ class TestFileTiff(PillowTestCase): self.assertEqual(im.tag_v2[X_RESOLUTION], 36) self.assertEqual(im.tag_v2[Y_RESOLUTION], 72) + def test_multipage_compression(self): + im = Image.open('Tests/images/compression.tif') + + im.seek(0) + self.assertEqual(im._compression,'tiff_ccitt') + self.assertEqual(im.size, (10, 10)) + + im.seek(1) + self.assertEqual(im._compression,'packbits') + self.assertEqual(im.size, (10, 10)) + im.load() + + im.seek(0) + self.assertEqual(im._compression,'tiff_ccitt') + self.assertEqual(im.size, (10, 10)) + im.load() if __name__ == '__main__': unittest.main()