Added is_animated

This commit is contained in:
Andrew Murray 2015-06-30 13:25:00 +10:00
parent 75be4af068
commit d20eef450b
17 changed files with 80 additions and 0 deletions

View File

@ -66,6 +66,10 @@ class DcxImageFile(PcxImageFile):
def n_frames(self): def n_frames(self):
return len(self._offset) return len(self._offset)
@property
def is_animated(self):
return len(self._offset) > 1
def seek(self, frame): def seek(self, frame):
if frame >= len(self._offset): if frame >= len(self._offset):
raise EOFError("attempt to seek outside DCX directory") raise EOFError("attempt to seek outside DCX directory")

View File

@ -90,6 +90,7 @@ class FliImageFile(ImageFile.ImageFile):
self.__fp = self.fp self.__fp = self.fp
self.__rewind = self.fp.tell() self.__rewind = self.fp.tell()
self._n_frames = None self._n_frames = None
self._is_animated = None
self.seek(0) self.seek(0)
def _palette(self, palette, shift): def _palette(self, palette, shift):
@ -122,6 +123,20 @@ class FliImageFile(ImageFile.ImageFile):
self.seek(current) self.seek(current)
return self._n_frames return self._n_frames
@property
def is_animated(self):
if self._is_animated is None:
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): def seek(self, frame):
if frame == self.__frame: if frame == self.__frame:
return return

View File

@ -88,6 +88,7 @@ class GifImageFile(ImageFile.ImageFile):
self.__fp = self.fp # FIXME: hack self.__fp = self.fp # FIXME: hack
self.__rewind = self.fp.tell() self.__rewind = self.fp.tell()
self._n_frames = None self._n_frames = None
self._is_animated = None
self._seek(0) # get ready to read first frame self._seek(0) # get ready to read first frame
@property @property
@ -102,6 +103,20 @@ class GifImageFile(ImageFile.ImageFile):
self.seek(current) self.seek(current)
return self._n_frames return self._n_frames
@property
def is_animated(self):
if self._is_animated is None:
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): def seek(self, frame):
if frame == self.__frame: if frame == self.__frame:
return return

View File

@ -264,6 +264,10 @@ class ImImageFile(ImageFile.ImageFile):
def n_frames(self): def n_frames(self):
return self.info[FRAMES] return self.info[FRAMES]
@property
def is_animated(self):
return self.info[FRAMES] > 1
def seek(self, frame): def seek(self, frame):
if frame < 0 or frame >= self.info[FRAMES]: if frame < 0 or frame >= self.info[FRAMES]:

View File

@ -75,6 +75,10 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
def n_frames(self): def n_frames(self):
return len(self.images) return len(self.images)
@property
def is_animated(self):
return len(self.images) > 1
def seek(self, frame): def seek(self, frame):
try: try:

View File

@ -66,6 +66,10 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
def n_frames(self): def n_frames(self):
return self.__framecount return self.__framecount
@property
def is_animated(self):
return self.__framecount > 1
def seek(self, frame): def seek(self, frame):
if frame < 0 or frame >= self.__framecount: if frame < 0 or frame >= self.__framecount:
raise EOFError("no more images in MPO file") raise EOFError("no more images in MPO file")

View File

@ -136,6 +136,10 @@ class PsdImageFile(ImageFile.ImageFile):
def n_frames(self): def n_frames(self):
return len(self.layers) return len(self.layers)
@property
def is_animated(self):
return len(self.layers) > 1
def seek(self, layer): def seek(self, layer):
# seek to given layer (1..max) # seek to given layer (1..max)
if layer == self.frame: if layer == self.frame:

View File

@ -158,6 +158,10 @@ class SpiderImageFile(ImageFile.ImageFile):
def n_frames(self): def n_frames(self):
return self._nimages return self._nimages
@property
def is_animated(self):
return self._nimages > 1
# 1st image index is zero (although SPIDER imgnumber starts at 1) # 1st image index is zero (although SPIDER imgnumber starts at 1)
def tell(self): def tell(self):
if self.imgnumber < 1: if self.imgnumber < 1:

View File

@ -650,6 +650,7 @@ class TiffImageFile(ImageFile.ImageFile):
self.__fp = self.fp self.__fp = self.fp
self._frame_pos = [] self._frame_pos = []
self._n_frames = None self._n_frames = None
self._is_animated = None
if Image.DEBUG: if Image.DEBUG:
print("*** TiffImageFile._open ***") print("*** TiffImageFile._open ***")
@ -671,6 +672,20 @@ class TiffImageFile(ImageFile.ImageFile):
self.seek(current) self.seek(current)
return self._n_frames return self._n_frames
@property
def is_animated(self):
if self._is_animated is None:
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): def seek(self, frame):
"Select a given frame as current image" "Select a given frame as current image"
self._seek(max(frame, 0)) # Questionable backwards compatibility. self._seek(max(frame, 0)) # Questionable backwards compatibility.

View File

@ -33,6 +33,7 @@ class TestFileDcx(PillowTestCase):
def test_n_frames(self): def test_n_frames(self):
im = Image.open(TEST_FILE) im = Image.open(TEST_FILE)
self.assertEqual(im.n_frames, 1) self.assertEqual(im.n_frames, 1)
self.assertFalse(im.is_animated)
def test_eoferror(self): def test_eoferror(self):
im = Image.open(TEST_FILE) im = Image.open(TEST_FILE)

View File

@ -21,6 +21,7 @@ class TestFileFli(PillowTestCase):
def test_n_frames(self): def test_n_frames(self):
im = Image.open(test_file) im = Image.open(test_file)
self.assertEqual(im.n_frames, 1) self.assertEqual(im.n_frames, 1)
self.assertFalse(im.is_animated)
def test_eoferror(self): def test_eoferror(self):
im = Image.open(test_file) im = Image.open(test_file)

View File

@ -137,9 +137,11 @@ class TestFileGif(PillowTestCase):
def test_n_frames(self): def test_n_frames(self):
im = Image.open(TEST_GIF) im = Image.open(TEST_GIF)
self.assertEqual(im.n_frames, 1) self.assertEqual(im.n_frames, 1)
self.assertFalse(im.is_animated)
im = Image.open("Tests/images/iss634.gif") im = Image.open("Tests/images/iss634.gif")
self.assertEqual(im.n_frames, 42) self.assertEqual(im.n_frames, 42)
self.assertTrue(im.is_animated)
def test_eoferror(self): def test_eoferror(self):
im = Image.open(TEST_GIF) im = Image.open(TEST_GIF)

View File

@ -18,6 +18,7 @@ class TestFileIm(PillowTestCase):
def test_n_frames(self): def test_n_frames(self):
im = Image.open(TEST_IM) im = Image.open(TEST_IM)
self.assertEqual(im.n_frames, 1) self.assertEqual(im.n_frames, 1)
self.assertFalse(im.is_animated)
def test_eoferror(self): def test_eoferror(self):
im = Image.open(TEST_IM) im = Image.open(TEST_IM)

View File

@ -98,6 +98,7 @@ class TestFileMpo(PillowTestCase):
def test_n_frames(self): def test_n_frames(self):
im = Image.open("Tests/images/sugarshack.mpo") im = Image.open("Tests/images/sugarshack.mpo")
self.assertEqual(im.n_frames, 2) self.assertEqual(im.n_frames, 2)
self.assertTrue(im.is_animated)
def test_eoferror(self): def test_eoferror(self):
im = Image.open("Tests/images/sugarshack.mpo") im = Image.open("Tests/images/sugarshack.mpo")

View File

@ -19,9 +19,11 @@ class TestImagePsd(PillowTestCase):
def test_n_frames(self): def test_n_frames(self):
im = Image.open("Tests/images/hopper_merged.psd") im = Image.open("Tests/images/hopper_merged.psd")
self.assertEqual(im.n_frames, 1) self.assertEqual(im.n_frames, 1)
self.assertFalse(im.is_animated)
im = Image.open(test_file) im = Image.open(test_file)
self.assertEqual(im.n_frames, 2) self.assertEqual(im.n_frames, 2)
self.assertTrue(im.is_animated)
def test_eoferror(self): def test_eoferror(self):
im = Image.open(test_file) im = Image.open(test_file)

View File

@ -45,6 +45,7 @@ class TestImageSpider(PillowTestCase):
def test_n_frames(self): def test_n_frames(self):
im = Image.open(TEST_FILE) im = Image.open(TEST_FILE)
self.assertEqual(im.n_frames, 1) self.assertEqual(im.n_frames, 1)
self.assertFalse(im.is_animated)
def test_loadImageSeries(self): def test_loadImageSeries(self):
# Arrange # Arrange

View File

@ -153,9 +153,11 @@ class TestFileTiff(PillowTestCase):
def test_n_frames(self): def test_n_frames(self):
im = Image.open('Tests/images/multipage-lastframe.tif') im = Image.open('Tests/images/multipage-lastframe.tif')
self.assertEqual(im.n_frames, 1) self.assertEqual(im.n_frames, 1)
self.assertFalse(im.is_animated)
im = Image.open('Tests/images/multipage.tiff') im = Image.open('Tests/images/multipage.tiff')
self.assertEqual(im.n_frames, 3) self.assertEqual(im.n_frames, 3)
self.assertTrue(im.is_animated)
def test_eoferror(self): def test_eoferror(self):
im = Image.open('Tests/images/multipage-lastframe.tif') im = Image.open('Tests/images/multipage-lastframe.tif')