Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type hints to ImageSequence #7635

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Changes from 1 commit
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
17 changes: 12 additions & 5 deletions src/PIL/ImageSequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
##
from __future__ import annotations

from typing import Callable

from . import Image


class Iterator:
"""
Expand All @@ -29,25 +33,25 @@ class Iterator:
:param im: An image object.
"""

def __init__(self, im):
def __init__(self, im: Image.Image):
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:
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
Expand All @@ -57,7 +61,10 @@ def __next__(self):
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.
Expand Down
Loading