Merge pull request #1782 from radarhere/compression

Different frames may have different compression types
This commit is contained in:
wiredfool 2016-04-01 03:38:18 -07:00
commit 26970c5e21
3 changed files with 29 additions and 8 deletions

View File

@ -238,8 +238,8 @@ class IFDRational(Rational):
as a fractions.Fraction(). Delegate as appropriate as a fractions.Fraction(). Delegate as appropriate
""" """
__slots__ = ('_numerator', '_denominator', '_val') __slots__ = ('_numerator', '_denominator', '_val')
def __init__(self, value, denominator=1): def __init__(self, value, denominator=1):
""" """
@ -255,7 +255,7 @@ class IFDRational(Rational):
self._numerator = value.numerator self._numerator = value.numerator
self._denominator = value.denominator self._denominator = value.denominator
self._val = value self._val = value
if type(value) == IFDRational: if type(value) == IFDRational:
self._denominator = value.denominator self._denominator = value.denominator
self._numerator = value.numerator self._numerator = value.numerator
@ -287,7 +287,7 @@ class IFDRational(Rational):
def limit_rational(self, max_denominator): def limit_rational(self, max_denominator):
""" """
:param max_denominator: Integer, the maximum denominator value :param max_denominator: Integer, the maximum denominator value
:returns: Tuple of (numerator, denominator) :returns: Tuple of (numerator, denominator)
""" """
@ -349,7 +349,7 @@ class IFDRational(Rational):
__floor__ = _delegate('__floor__') __floor__ = _delegate('__floor__')
__round__ = _delegate('__round__') __round__ = _delegate('__round__')
class ImageFileDirectory_v2(collections.MutableMapping): class ImageFileDirectory_v2(collections.MutableMapping):
"""This class represents a TIFF tag directory. To speed things up, we """This class represents a TIFF tag directory. To speed things up, we
@ -991,6 +991,11 @@ class TiffImageFile(ImageFile.ImageFile):
return args return args
def load(self):
if self.use_load_libtiff:
return self._load_libtiff()
return super(TiffImageFile, self).load()
def _load_libtiff(self): def _load_libtiff(self):
""" Overload method triggered when we detect a compressed tiff """ Overload method triggered when we detect a compressed tiff
Calls out to libtiff """ Calls out to libtiff """
@ -1136,6 +1141,7 @@ class TiffImageFile(ImageFile.ImageFile):
# build tile descriptors # build tile descriptors
x = y = l = 0 x = y = l = 0
self.tile = [] self.tile = []
self.use_load_libtiff = False
if STRIPOFFSETS in self.tag_v2: if STRIPOFFSETS in self.tag_v2:
# striped image # striped image
offsets = self.tag_v2[STRIPOFFSETS] offsets = self.tag_v2[STRIPOFFSETS]
@ -1158,10 +1164,9 @@ class TiffImageFile(ImageFile.ImageFile):
# function. # function.
# #
# Setup the one tile for the whole image, then # Setup the one tile for the whole image, then
# replace the existing load function with our # use the _load_libtiff function.
# _load_libtiff function.
self.load = self._load_libtiff self.use_load_libtiff = True
# To be nice on memory footprint, if there's a # To be nice on memory footprint, if there's a
# file descriptor, use that instead of reading # file descriptor, use that instead of reading

BIN
Tests/images/compression.tif Executable file

Binary file not shown.

View File

@ -378,6 +378,22 @@ class TestFileTiff(PillowTestCase):
self.assertEqual(im.tag_v2[X_RESOLUTION], 36) self.assertEqual(im.tag_v2[X_RESOLUTION], 36)
self.assertEqual(im.tag_v2[Y_RESOLUTION], 72) 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__': if __name__ == '__main__':
unittest.main() unittest.main()