Skip to content

Commit

Permalink
Merge pull request #1782 from radarhere/compression
Browse files Browse the repository at this point in the history
Different frames may have different compression types
  • Loading branch information
wiredfool committed Apr 1, 2016
2 parents 39fb128 + 33fff9e commit 26970c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
21 changes: 13 additions & 8 deletions PIL/TiffImagePlugin.py
Expand Up @@ -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):
"""
Expand All @@ -255,7 +255,7 @@ def __init__(self, value, denominator=1):
self._numerator = value.numerator
self._denominator = value.denominator
self._val = value

if type(value) == IFDRational:
self._denominator = value.denominator
self._numerator = value.numerator
Expand Down Expand Up @@ -287,7 +287,7 @@ def denominator(a):

def limit_rational(self, max_denominator):
"""
:param max_denominator: Integer, the maximum denominator value
:returns: Tuple of (numerator, denominator)
"""
Expand Down Expand Up @@ -349,7 +349,7 @@ def delegate(self, *args):
__floor__ = _delegate('__floor__')
__round__ = _delegate('__round__')



class ImageFileDirectory_v2(collections.MutableMapping):
"""This class represents a TIFF tag directory. To speed things up, we
Expand Down Expand Up @@ -991,6 +991,11 @@ def _decoder(self, rawmode, layer, tile=None):

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 """
Expand Down Expand Up @@ -1136,6 +1141,7 @@ def _setup(self):
# 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]
Expand All @@ -1158,10 +1164,9 @@ def _setup(self):
# 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
Expand Down
Binary file added Tests/images/compression.tif
Binary file not shown.
16 changes: 16 additions & 0 deletions Tests/test_file_tiff.py
Expand Up @@ -378,6 +378,22 @@ def test_deprecation_warning_with_spaces(self):
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()
Expand Down

0 comments on commit 26970c5

Please sign in to comment.