openmedialibrary_platform_d.../lib/python3.7/site-packages/PIL/ImageSequence.py

57 lines
1.2 KiB
Python
Raw Normal View History

2016-02-06 09:36:57 +00:00
#
# The Python Imaging Library.
# $Id$
#
# sequence support classes
#
# history:
# 1997-02-20 fl Created
#
# Copyright (c) 1997 by Secret Labs AB.
# Copyright (c) 1997 by Fredrik Lundh.
#
# See the README file for information on usage and redistribution.
#
##
class Iterator(object):
"""
This class implements an iterator object that can be used to loop
over an image sequence.
You can use the ``[]`` operator to access elements by index. This operator
will raise an :py:exc:`IndexError` if you try to access a nonexistent
frame.
:param im: An image object.
"""
def __init__(self, im):
if not hasattr(im, "seek"):
raise AttributeError("im must have seek method")
self.im = im
2018-12-31 23:25:26 +00:00
self.position = 0
2016-02-06 09:36:57 +00:00
def __getitem__(self, ix):
try:
2018-12-31 23:25:26 +00:00
self.im.seek(ix)
2016-02-06 09:36:57 +00:00
return self.im
except EOFError:
raise IndexError # end of sequence
2018-12-31 23:25:26 +00:00
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__()