updates
This commit is contained in:
@@ -14,6 +14,13 @@
|
||||
#
|
||||
|
||||
##
|
||||
from __future__ import annotations
|
||||
|
||||
from . import Image
|
||||
|
||||
TYPE_CHECKING = False
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable
|
||||
|
||||
|
||||
class Iterator:
|
||||
@@ -28,33 +35,38 @@ class Iterator:
|
||||
:param im: An image object.
|
||||
"""
|
||||
|
||||
def __init__(self, im):
|
||||
def __init__(self, im: Image.Image) -> None:
|
||||
if not hasattr(im, "seek"):
|
||||
msg = "im must have seek method"
|
||||
raise AttributeError(msg)
|
||||
self.im = im
|
||||
self.position = getattr(self.im, "_min_frame", 0)
|
||||
|
||||
def __getitem__(self, ix):
|
||||
def __getitem__(self, ix: int) -> Image.Image:
|
||||
try:
|
||||
self.im.seek(ix)
|
||||
return self.im
|
||||
except EOFError as e:
|
||||
raise IndexError from e # end of sequence
|
||||
msg = "end of sequence"
|
||||
raise IndexError(msg) from e
|
||||
|
||||
def __iter__(self):
|
||||
def __iter__(self) -> Iterator:
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
def __next__(self) -> Image.Image:
|
||||
try:
|
||||
self.im.seek(self.position)
|
||||
self.position += 1
|
||||
return self.im
|
||||
except EOFError as e:
|
||||
raise StopIteration from e
|
||||
msg = "end of sequence"
|
||||
raise StopIteration(msg) from e
|
||||
|
||||
|
||||
def all_frames(im, func=None):
|
||||
def all_frames(
|
||||
im: Image.Image | list[Image.Image],
|
||||
func: Callable[[Image.Image], Image.Image] | None = None,
|
||||
) -> list[Image.Image]:
|
||||
"""
|
||||
Applies a given function to all frames in an image or a list of images.
|
||||
The frames are returned as a list of separate images.
|
||||
|
||||
Reference in New Issue
Block a user