Merge pull request #3714 from radarhere/tiff_frames

Improvements to TIFF is_animated and n_frames
This commit is contained in:
Hugo 2019-03-30 12:04:16 +02:00 committed by GitHub
commit 05849ca794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 29 deletions

View File

@ -253,11 +253,6 @@ class TestFileTiff(PillowTestCase):
['Tests/images/multipage-lastframe.tif', 1],
['Tests/images/multipage.tiff', 3]
]:
# Test is_animated before n_frames
im = Image.open(path)
self.assertEqual(im.is_animated, n_frames != 1)
# Test is_animated after n_frames
im = Image.open(path)
self.assertEqual(im.n_frames, n_frames)
self.assertEqual(im.is_animated, n_frames != 1)

View File

@ -985,7 +985,6 @@ class TiffImageFile(ImageFile.ImageFile):
self.__fp = self.fp
self._frame_pos = []
self._n_frames = None
self._is_animated = None
if DEBUG:
print("*** TiffImageFile._open ***")
@ -999,29 +998,14 @@ class TiffImageFile(ImageFile.ImageFile):
def n_frames(self):
if self._n_frames is None:
current = self.tell()
try:
while True:
self._seek(self.tell() + 1)
except EOFError:
self._n_frames = self.tell() + 1
self._seek(len(self._frame_pos))
while self._n_frames is None:
self._seek(self.tell() + 1)
self.seek(current)
return self._n_frames
@property
def is_animated(self):
if self._is_animated is None:
if self._n_frames is not None:
self._is_animated = self._n_frames != 1
else:
current = self.tell()
try:
self.seek(1)
self._is_animated = True
except EOFError:
self._is_animated = False
self.seek(current)
return self._is_animated
def seek(self, frame):
@ -1053,6 +1037,10 @@ class TiffImageFile(ImageFile.ImageFile):
print("Loading tags, location: %s" % self.fp.tell())
self.tag_v2.load(self.fp)
self.__next = self.tag_v2.next
if self.__next == 0:
self._n_frames = frame + 1
if len(self._frame_pos) == 1:
self._is_animated = self.__next != 0
self.__frame += 1
self.fp.seek(self._frame_pos[frame])
self.tag_v2.load(self.fp)
@ -1086,7 +1074,7 @@ class TiffImageFile(ImageFile.ImageFile):
def load_end(self):
# allow closing if we're on the first frame, there's no next
# This is the ImageFile.load path only, libtiff specific below.
if len(self._frame_pos) == 1 and not self.__next:
if not self._is_animated:
self._close_exclusive_fp_after_loading = True
def _load_libtiff(self):
@ -1166,10 +1154,9 @@ class TiffImageFile(ImageFile.ImageFile):
self.tile = []
self.readonly = 0
# libtiff closed the fp in a, we need to close self.fp, if possible
if self._exclusive_fp:
if len(self._frame_pos) == 1 and not self.__next:
self.fp.close()
self.fp = None # might be shared
if self._exclusive_fp and not self._is_animated:
self.fp.close()
self.fp = None # might be shared
if err < 0:
raise IOError(err)