Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,6 @@ def test_n_frames(self):
['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)
Expand Down
35 changes: 11 additions & 24 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,6 @@ def _open(self):
self.__fp = self.fp
self._frame_pos = []
self._n_frames = None
self._is_animated = None

if DEBUG:
print("*** TiffImageFile._open ***")
Expand All @@ -999,29 +998,14 @@ def _open(self):
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):
Expand Down Expand Up @@ -1053,6 +1037,10 @@ def _seek(self, frame):
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)
Expand Down Expand Up @@ -1086,7 +1074,7 @@ def load(self):
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):
Expand Down Expand Up @@ -1166,10 +1154,9 @@ def _load_libtiff(self):
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)
Expand Down