You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vidiopy/video/ImageSequenceClip.py
+158-5Lines changed: 158 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,34 @@
1
1
importos
2
2
importmath
3
3
frompathlibimportPath
4
-
fromtypingimportCallable, Self, Any
4
+
fromtypingimportCallable
5
5
fromPILimportImage
6
6
importnumpyasnp
7
7
from ..decoratorsimport*
8
8
from .VideoClipimportVideoClip
9
9
10
10
11
11
classImageSequenceClip(VideoClip):
12
+
"""
13
+
A class used to represent a sequence of images as a video clip.
14
+
15
+
This class extends the VideoClip class and provides additional functionality for handling sequences of images. It allows for the creation of a video clip from a sequence of images, with the ability to specify the frames per second (fps) and duration of the clip. The sequence of images can be provided as a tuple of PIL Images, paths to images, numpy arrays, or a path to a directory. The class also provides methods for importing the image sequence, generating a numpy array or PIL Image representation of a specific frame in the clip, and applying a function to each frame of the clip.
16
+
17
+
Attributes:
18
+
clip (tuple[Image.Image, ...]): The sequence of images as a tuple of PIL Images.
19
+
fps (int | float | None): The frames per second of the clip.
20
+
_dur (int | float | None): The duration of the clip in seconds.
21
+
audio (optional): The audio of the clip.
22
+
23
+
Methods:
24
+
__init__(sequence, fps=None, duration=None, audio=None): Initializes an instance of the ImageSequenceClip class.
25
+
__eq__(other): Checks if this instance is equal to another instance.
26
+
_import_image_sequence(sequence): Imports an image sequence from a tuple of PIL Images, paths to images, numpy arrays, or a path to a directory.
27
+
make_frame_array(t): Generates a numpy array representation of a specific frame in the clip.
28
+
make_frame_pil(t): Generates a PIL Image representation of a specific frame in the clip.
29
+
fl_frame_transform(func, *args, **kwargs): Applies a function to each frame of the clip.
30
+
fl_clip_transform(func, *args, **kwargs): Applies a function to each frame of the clip along with its timestamp.
31
+
"""
12
32
def__init__(
13
33
self,
14
34
sequence: (
@@ -22,6 +42,28 @@ def __init__(
22
42
duration: int|float|None=None,
23
43
audio=None,
24
44
):
45
+
"""
46
+
Initializes an instance of the ImageSequenceClip class.
47
+
48
+
This method imports an image sequence from the specified sequence, sets the fps and duration of the image sequence clip, and sets the audio of the image sequence clip if specified.
49
+
50
+
Args:
51
+
sequence (str | Path | tuple[Image.Image, ...] | tuple[np.ndarray, ...] | tuple[str | Path, ...]): The sequence to import. It can be a tuple of PIL Images, paths to images, numpy arrays, or a path to a directory.
52
+
fps (int | float | None, optional): The frames per second of the image sequence clip. If not specified, it is calculated from the duration and the number of images in the sequence.
53
+
duration (int | float | None, optional): The duration of the image sequence clip in seconds. If not specified, it is calculated from the fps and the number of images in the sequence.
54
+
audio (optional): The audio of the image sequence clip. If not specified, the image sequence clip will have no audio.
55
+
56
+
Raises:
57
+
ValueError: If neither fps nor duration is specified.
58
+
ValueError: If not all images in the sequence have the same size.
Imports an image sequence from a tuple of PIL Images, paths to images, numpy arrays, or a path to a directory.
111
+
112
+
This method checks the type of the sequence argument and imports the image sequence accordingly. If the sequence is a tuple of PIL Images, it returns the sequence as is. If the sequence is a tuple of numpy arrays, it converts each numpy array to a PIL Image. If the sequence is a tuple of paths to images, it opens each image and returns a tuple of PIL Images. If the sequence is a path to a directory, it opens all images in the directory and returns a tuple of PIL Images.
113
+
114
+
Args:
115
+
sequence (str | Path | tuple[str | Path] | tuple[Image.Image] | tuple[np.ndarray]): The sequence to import. It can be a tuple of PIL Images, paths to images, numpy arrays, or a path to a directory.
116
+
117
+
Returns:
118
+
tuple[Image.Image, ...]: The imported image sequence as a tuple of PIL Images.
119
+
120
+
Raises:
121
+
TypeError: If the sequence is not a tuple of PIL Images, paths to images, numpy arrays, or a path to a directory.
Applies a function to each frame of the image sequence clip.
220
+
221
+
This method iterates over each frame in the image sequence clip, applies a function to it, and replaces the original frame with the result. The function is expected to take a PIL Image as its first argument and return a PIL Image.
222
+
223
+
Args:
224
+
func (Callable[..., Image.Image]): The function to apply to each frame. It should take a PIL Image as its first argument and return a PIL Image.
225
+
*args: Additional positional arguments to pass to the function.
226
+
**kwargs: Additional keyword arguments to pass to the function.
227
+
228
+
Returns:
229
+
ImageSequenceClip: The current instance of the ImageSequenceClip class.
Applies a function to each frame of the image sequence clip along with its timestamp.
254
+
255
+
This method iterates over each frame in the image sequence clip, applies a function to it and its timestamp, and replaces the original frame with the result. The function is expected to take a PIL Image and a float as its first two arguments and return a PIL Image.
256
+
257
+
Args:
258
+
func (Callable[..., Image.Image]): The function to apply to each frame. It should take a PIL Image and a float as its first two arguments and return a PIL Image.
259
+
*args: Additional positional arguments to pass to the function.
260
+
**kwargs: Additional keyword arguments to pass to the function.
261
+
262
+
Returns:
263
+
ImageSequenceClip: The current instance of the ImageSequenceClip class.
264
+
265
+
Raises:
266
+
ValueError: If the fps of the image sequence clip is not set.
0 commit comments