Skip to content

ImageClip

class vidiopy.ImageClip(image: str | Path | Image.Image | np.ndarray | None = None, fps: int | float | None = None, duration: int | float | None = None)

Bases: vidiopy.VideoClip

All Methods and properties of the VideoClip class are available.

A class representing a video clip generated from a single image.

Parameters:
  • image: str | Path | Image.Image | np.ndarray | None: The image to use for the video clip. If None, an empty video clip is created.
  • fps: int | float | None: The frames per second of the video clip. If None, the fps is set to 30.
  • duration: int | float | None: The duration of the video clip in seconds. If None, the duration is set to 1.
Attributes:
  • image: Image.Image: The image used for the video clip.
  • Other attributes are inherited from the VideoClip class.
Methods:

_import_image(self, image) -> Image.Image:

Import the image from various sources.

Does not made for external use.

Parameters:
image (str | Path | Image.Image | np.ndarray): Input image data.
Returns:
Image.Image: The imported image data.

This is a private method and not intended for external use.

You Can Use set_duration() & duration property to change _dur.

fl_frame_transform(self, func, *args, **kwargs) -> Self:

Apply a frame transformation function to the image.

Parameters:
func (Callable): The frame transformation function.
*args: Additional positional arguments for the function.
**kwargs: Additional keyword arguments for the function.
Returns:
ImageClip: A new ImageClip instance with the transformed image.
Note:
This method modifies the current ImageClip instance in-place.
Example Usage:
image_clip = ImageClip(image_path, fps=30, duration=5.0)
transformed_clip = image_clip.fl_frame_transform(resize, width=640, height=480)

fl_frame_transform(self, func, *args, **kwargs) -> Self:

Apply a frame transformation function to the image.

Parameters:
func (Callable): The frame transformation function.
*args: Additional positional arguments for the function.
**kwargs: Additional keyword arguments for the function.
Returns:
ImageClip: A new ImageClip instance with the transformed image.
Note:
This method modifies the current ImageClip instance in-place.
Example Usage:
image_clip = ImageClip(image_path, fps=30, duration=5.0)
transformed_clip = image_clip.fl_frame_transform(resize, width=640, height=480)

fl_clip_transform(self, func, *args, **kwargs) -> Self:

Raise a ValueError indicating that fl_clip is not applicable for ImageClip.

The Clip should be converted to VideoClip using to_video_clip method first.

Parameters:
func: Unused.
*args: Unused.
**kwargs: Unused.
Returns:
ImageClip: The current ImageClip instance.
Raises:
ValueError: This method is not applicable for ImageClip.
Example Usage:
image_clip = ImageClip(image_path, fps=30, duration=5.0)
image_clip.fl_clip(some_function)  # Raises ValueError

fx(self, func: Callable, *args, **kwargs):

Apply a generic function to the ImageClip.

Parameters:
func (Callable): The function to apply.
*args: Additional positional arguments for the function.
**kwargs: Additional keyword arguments for the function.
Returns:
ImageClip: The current ImageClip instance.
Note:
This method modifies the current ImageClip instance in-place.
Example Usage:
def custom_function(image):
    # Some custom processing on the image
    return modified_image

image_clip = ImageClip(image_path, fps=30, duration=5.0)
image_clip.fx(custom_function, some_arg=42)

sub_fx(self, func, *args, start_t: int | float | None = None, end_t: int | float | None = None, **kwargs) -> Self:

Apply a custom function to the Image Clip.

Note:
Before using the sub_fx method, you need to convert the image clip to a video clip using to_video_clip() function.
Args:
func: The custom function to apply to the Image Clip.
*args: Additional positional arguments to pass to the custom function.
start_t (int | float | None): The start time of the subclip in seconds. If None, the subclip starts from the beginning.
end_t (int | float | None): The end time of the subclip in seconds. If None, the subclip ends at the last frame.
**kwargs: Additional keyword arguments to pass to the custom function.
Returns:
Self: The modified ImageClips instance.
Example:
# Convert the image clip to a video clip
video_clip = image_clip.to_video_clip()

# Apply a custom function to the video clip
modified_clip = video_clip.sub_fx(custom_function, start_t=2, end_t=5)
Raises:
ValueError: If the method is called on an Image Clip instead of a Video Clip.

sub_clip_copy(self, start: int | float | None = None, end: int | float | None = None) -> Self:

Create a copy of the current clip and apply sub-clip operation. Read more about sub-clip operation in the sub_clip method.

Args:
start (int | float | None): Start time of the sub-clip in seconds. If None, the sub-clip starts from the beginning of the original clip.
end (int | float | None): End time of the sub-clip in seconds. If None, the sub-clip ends at the end of the original clip.
Returns:
Self: A new instance of the clip with the sub-clip applied.
Example:
image_clip = ImageClip(image_path, fps=30, duration=5.0)
sub_clip = image_clip.sub_clip_copy(start=2, end=5)

sub_clip(self, start: int | float | None = None, end: int | float | None = None) -> Self:

Returns a sub-clip of the current clip.

Args:
start (int | float | None, optional): The start time of the sub-clip in seconds. Defaults to None.
end (int | float | None, optional): The end time of the sub-clip in seconds. Defaults to None.
Returns:
Self: The sub-clip.
Note:
It modifies the current clip in-place. If both start and end are None, the original clip is returned. If start is None, it defaults to 0. If end is None, it defaults to the end time of the original clip.
Example:
image_clip = ImageClip(image_path, fps=30, duration=5.0)
image_clip.sub_clip(start=2, end=5)

make_frame_array(self, t):

Gives the numpy array representation of the image at a given time.

Args:
t (float): The timestamp of the frame.
Returns:
numpy.ndarray: The numpy array representation of the image.
Raises:
ValueError: If the image is not set.

make_frame_pil(self, t) -> Image.Image:

Returns the image frame at a given time.

Args:
t (float): The time at which to retrieve the frame.
Returns:
PIL.Image.Image: The image frame at the given time.
Raises:
ValueError: If the image is not set.

to_video_clip(self, fps=None, duration=None):

Convert ImageClip to VideoClip

If fps or duration is not provided, it defaults to the corresponding attribute of the ImageClip instance. If those attributes are not available, a ValueError is raised.

Parameters:
fps (float, optional): Frames per second of the resulting video clip. If not provided, it defaults to the fps attribute of the ImageClip instance. If that is also not available, a ValueError is raised.
duration (float, optional): Duration of the resulting video clip in seconds. If not provided, it defaults to the duration attribute of the ImageClip instance. If that is also not available, a ValueError is raised.
Returns:
ImageSequenceClip: A VideoClip subclass instance generated from the ImageClip frames.
Raises:
ValueError: If fps or duration is not provided and the corresponding attribute is not available.
Note:
The to_video_clip method returns an instance of the ImageSequenceClip class, which is a subclass of the VideoClip Class.
Example Usage:
# Example Usage
image_clip = ImageClip()
video_clip = image_clip.to_video_clip(fps=24, duration=10.0)
video_clip.sub_fx(custom_function, start_t=2, end_t=5)

Data2ImageClip

class vidiopy.Data2ImageClip(data: np.ndarray | Image.Image, fps: int | float | None = None, duration: int | float | None = None)

Bases: vidiopy.ImageClip

A class representing a video clip generated from raw data (numpy array or PIL Image).

It extends the ImageClip class and allows users to create video clips from raw data, supporting either numpy arrays or PIL Images as input.

Parameters:
  • data (np.ndarray or PIL Image): The raw data to be converted into a video clip.
  • fps (int | float | None): Frames per second of the video. If not provided, it will be inherited from the parent class (ImageClip) or set to the default value.
  • duration (int | float | None): Duration of the video in seconds. If not provided, it will be inherited from the parent class (ImageClip) or set to the default value.
Attributes:
  • image (PIL Image): The PIL Image representation of the provided data.
  • size (tuple): The size (width, height) of the image.
Methods:

_import_image(self, image) -> Image.Image:

Private method to convert the provided data (numpy array or PIL Image) into a PIL Image.

Parameters:
image (np.ndarray or PIL Image): The raw data to be converted.
Returns:
Image.Image: The PIL Image representation of the provided data.
Raises:
TypeError: If the input type is not supported (neither numpy array nor PIL Image).
Example Usage:
# Import necessary libraries

# Create a Data2ImageClip instance from a numpy array
data_array = np.random.randint(0, 255, size=(480, 640, 3), dtype=np.uint8)
video_clip = Data2ImageClip(data=data_array, fps=30, duration=5)

# Create a Data2ImageClip instance from a PIL Image
from PIL import Image
data_image = Image.new('RGB', (640, 480), color='red')
video_clip = Data2ImageClip(data=data_image, fps=24, duration=10)
Note:

The Data2ImageClip class extends the ImageClip. It allows users to create video clips from raw data, supporting either numpy arrays or PIL Images as input.

ColorClip

class vidiopy.ColorClip(color: str | tuple[int, ...], mode="RGBA", size=(1, 1), fps=None, duration=None)

Bases: #!py vidiopy.Data2ImageClip

A video clip class with a solid color.

It extends the Data2ImageClip class and allows users to create video clips with a solid color.

Parameters:
  • color: str | tuple[int, ...]: Color of the image. It can be a color name (e.g., 'red', 'blue') or RGB tuple.

    Available Color Names
    • aliceblue: "#f0f8ff",
    • antiquewhite: "#faebd7",
    • aqua: "#00ffff",
    • aquamarine: "#7fffd4",
    • azure: "#f0ffff",
    • beige: "#f5f5dc",
    • bisque: "#ffe4c4",
    • black: "#000000",
    • blanchedalmond: "#ffebcd",
    • blue: "#0000ff",
    • blueviolet: "#8a2be2",
    • brown: "#a52a2a",
    • burlywood: "#deb887",
    • cadetblue: "#5f9ea0",
    • chartreuse: "#7fff00",
    • chocolate: "#d2691e",
    • coral: "#ff7f50",
    • cornflowerblue: "#6495ed",
    • cornsilk: "#fff8dc",
    • crimson: "#dc143c",
    • cyan: "#00ffff",
    • darkblue: "#00008b",
    • darkcyan: "#008b8b",
    • darkgoldenrod: "#b8860b",
    • darkgray: "#a9a9a9",
    • darkgrey: "#a9a9a9",
    • darkgreen: "#006400",
    • darkkhaki: "#bdb76b",
    • darkmagenta: "#8b008b",
    • darkolivegreen: "#556b2f",
    • darkorange: "#ff8c00",
    • darkorchid: "#9932cc",
    • darkred: "#8b0000",
    • darksalmon: "#e9967a",
    • darkseagreen: "#8fbc8f",
    • darkslateblue: "#483d8b",
    • darkslategray: "#2f4f4f",
    • darkslategrey: "#2f4f4f",
    • darkturquoise: "#00ced1",
    • darkviolet: "#9400d3",
    • deeppink: "#ff1493",
    • deepskyblue: "#00bfff",
    • dimgray: "#696969",
    • dimgrey: "#696969",
    • dodgerblue: "#1e90ff",
    • firebrick: "#b22222",
    • floralwhite: "#fffaf0",
    • forestgreen: "#228b22",
    • fuchsia: "#ff00ff",
    • gainsboro: "#dcdcdc",
    • ghostwhite: "#f8f8ff",
    • gold: "#ffd700",
    • goldenrod: "#daa520",
    • gray: "#808080",
    • grey: "#808080",
    • green: "#008000",
    • greenyellow: "#adff2f",
    • honeydew: "#f0fff0",
    • hotpink: "#ff69b4",
    • indianred: "#cd5c5c",
    • indigo: "#4b0082",
    • ivory: "#fffff0",
    • khaki: "#f0e68c",
    • lavender: "#e6e6fa",
    • lavenderblush: "#fff0f5",
    • lawngreen: "#7cfc00",
    • lemonchiffon: "#fffacd",
    • lightblue: "#add8e6",
    • lightcoral: "#f08080",
    • lightcyan: "#e0ffff",
    • lightgoldenrodyellow: "#fafad2",
    • lightgreen: "#90ee90",
    • lightgray: "#d3d3d3",
    • lightgrey: "#d3d3d3",
    • lightpink: "#ffb6c1",
    • lightsalmon: "#ffa07a",
    • lightseagreen: "#20b2aa",
    • lightskyblue: "#87cefa",
    • lightslategray: "#778899",
    • lightslategrey: "#778899",
    • lightsteelblue: "#b0c4de",
    • lightyellow: "#ffffe0",
    • lime: "#00ff00",
    • limegreen: "#32cd32",
    • linen: "#faf0e6",
    • magenta: "#ff00ff",
    • maroon: "#800000",
    • mediumaquamarine: "#66cdaa",
    • mediumblue: "#0000cd",
    • mediumorchid: "#ba55d3",
    • mediumpurple: "#9370db",
    • mediumseagreen: "#3cb371",
    • mediumslateblue: "#7b68ee",
    • mediumspringgreen: "#00fa9a",
    • mediumturquoise: "#48d1cc",
    • mediumvioletred: "#c71585",
    • midnightblue: "#191970",
    • mintcream: "#f5fffa",
    • mistyrose: "#ffe4e1",
    • moccasin: "#ffe4b5",
    • navajowhite: "#ffdead",
    • navy: "#000080",
    • oldlace: "#fdf5e6",
    • olive: "#808000",
    • olivedrab: "#6b8e23",
    • orange: "#ffa500",
    • orangered: "#ff4500",
    • orchid: "#da70d6",
    • palegoldenrod: "#eee8aa",
    • palegreen: "#98fb98",
    • paleturquoise: "#afeeee",
    • palevioletred: "#db7093",
    • papayawhip: "#ffefd5",
    • peachpuff: "#ffdab9",
    • peru: "#cd853f",
    • pink: "#ffc0cb",
    • plum: "#dda0dd",
    • powderblue: "#b0e0e6",
    • purple: "#800080",
    • rebeccapurple: "#663399",
    • red: "#ff0000",
    • rosybrown: "#bc8f8f",
    • royalblue: "#4169e1",
    • saddlebrown: "#8b4513",
    • salmon: "#fa8072",
    • sandybrown: "#f4a460",
    • seagreen: "#2e8b57",
    • seashell: "#fff5ee",
    • sienna: "#a0522d",
    • silver: "#c0c0c0",
    • skyblue: "#87ceeb",
    • slateblue: "#6a5acd",
    • slategray: "#708090",
    • slategrey: "#708090",
    • snow: "#fffafa",
    • springgreen: "#00ff7f",
    • steelblue: "#4682b4",
    • tan: "#d2b48c",
    • teal: "#008080",
    • thistle: "#d8bfd8",
    • tomato: "#ff6347",
    • turquoise: "#40e0d0",
    • violet: "#ee82ee",
    • wheat: "#f5deb3",
    • white: "#ffffff",
    • whitesmoke: "#f5f5f5",
    • yellow: "#ffff00",
    • yellowgreen: "#9acd32",
  • mode: str: Mode to use for the image. Default is 'RGBA'.

  • size: tuple: Size of the image in pixels (width, height). Default is (1, 1) for changing size afterwards.
  • fps: float, optional: Frames per second for the video clip.
  • duration: float, optional: Duration of the video clip in seconds.
Attributes:
  • color: str | tuple[int, ...]: The color of the video clip.
  • mode: str: The mode of the video clip.
  • Other attributes are inherited from the Data2ImageClip class.
Methods:

set_size(self, size: tuple[int, int]):

Set the size of the video clip.

Parameters:
size: tuple[int, int]: New size of the video clip in pixels (width, height).
Example Usage:
color_clip.set_size((800, 600))
Example Usage:
# Create a red square video clip (500x500, 30 FPS, 5 seconds):
red_square = ColorClip(color='red', size=(500, 500), fps=30, duration=5)

# Create a blue fullscreen video clip (1920x1080, default FPS and duration):
blue_fullscreen = ColorClip(color='blue', size=(1920, 1080))

# Create a green transparent video clip (RGBA mode, 800x600):
green_transparent = ColorClip(color=(0, 255, 0, 0), mode='RGBA', size=(800, 600))

TextClip

class vidiopy.TextClip(text: str, font_pth: None | str = None, font_size: int = 20, txt_color: str | tuple[int, ...] = (255, 255, 255, 0), bg_color: str | tuple[int, ...] = (0, 0, 0, 0), fps=None, duration=None)

Bases: #!py vidiopy.Data2ImageClip

A class representing a text clip to be used in video compositions.

Parameters:
  • text (str): The text content to be displayed in the clip.
  • font_pth (None | str, optional): The file path to the TrueType font file (.ttf). If None, the default system font is used. Defaults to None.
  • font_size (int, optional): The font size for the text. Defaults to 20.
  • txt_color (str | tuple[int, ...], optional): The color of the text specified as either a string (e.g., 'white') or a tuple representing RGBA values. Defaults to (255, 255, 255, 0) (fully transparent white).
  • bg_color (str | tuple[int, ...], optional): The background color of the text clip, specified as either a string (e.g., 'black') or a tuple representing RGBA values. Defaults to (0, 0, 0, 0) (fully transparent black).
  • fps (float, optional): Frames per second of the video. If None, the value is inherited from the parent class. Defaults to None.
  • duration (float, optional): Duration of the video clip in seconds. If None, the value is inherited from the parent class. Defaults to None.
Attributes:
  • font (PIL.ImageFont.FreeTypeFont): The font object used for rendering the text.
  • image (PIL.Image.Image): The image containing the rendered text.
  • fps (float): Frames per second of the video clip.
  • duration (float): Duration of the video clip in seconds.
  • Other attributes are inherited from the Data2ImageClip class.
Example Usage:
# Create a TextClip with custom text and styling
text_clip = TextClip("Contribute to Vidiopy", font_size=30, txt_color='red', bg_color='blue', fps=24, duration=5.0)

# Use the text clip in a video composition
composition = CompositeVideoClip([other_clip, text_clip])
composition.write_videofile("output.mp4", codec='libx264', fps=24)

Comments