mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-26 09:14:27 +03:00
Modified ImageSequence Iterator class to be an iterator
This commit is contained in:
parent
f57e295ce5
commit
f4df96816b
|
@ -32,6 +32,7 @@ class Iterator(object):
|
||||||
if not hasattr(im, "seek"):
|
if not hasattr(im, "seek"):
|
||||||
raise AttributeError("im must have seek method")
|
raise AttributeError("im must have seek method")
|
||||||
self.im = im
|
self.im = im
|
||||||
|
self.position = 0
|
||||||
|
|
||||||
def __getitem__(self, ix):
|
def __getitem__(self, ix):
|
||||||
try:
|
try:
|
||||||
|
@ -40,3 +41,17 @@ class Iterator(object):
|
||||||
return self.im
|
return self.im
|
||||||
except EOFError:
|
except EOFError:
|
||||||
raise IndexError # end of sequence
|
raise IndexError # end of sequence
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
try:
|
||||||
|
self.im.seek(self.position)
|
||||||
|
self.position += 1
|
||||||
|
return self.im
|
||||||
|
except EOFError:
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
return self.__next__()
|
||||||
|
|
|
@ -24,6 +24,14 @@ class TestImageSequence(PillowTestCase):
|
||||||
|
|
||||||
self.assertRaises(AttributeError, lambda: ImageSequence.Iterator(0))
|
self.assertRaises(AttributeError, lambda: ImageSequence.Iterator(0))
|
||||||
|
|
||||||
|
def test_iterator(self):
|
||||||
|
im = Image.open('Tests/images/multipage.tiff')
|
||||||
|
i = ImageSequence.Iterator(im)
|
||||||
|
for index in range(0, im.n_frames):
|
||||||
|
self.assertEqual(i[index], next(i))
|
||||||
|
self.assertRaises(IndexError, lambda: i[index+1])
|
||||||
|
self.assertRaises(StopIteration, lambda: next(i))
|
||||||
|
|
||||||
def _test_multipage_tiff(self):
|
def _test_multipage_tiff(self):
|
||||||
im = Image.open('Tests/images/multipage.tiff')
|
im = Image.open('Tests/images/multipage.tiff')
|
||||||
for index, frame in enumerate(ImageSequence.Iterator(im)):
|
for index, frame in enumerate(ImageSequence.Iterator(im)):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user