zea.io_libΒΆ
Input / output functions for reading and writing files.
Use to quickly read and write files or interact with file system.
Functions
|
Computes a global color palette for a sequence of images using histogram analysis. |
|
Converts a grayscale image to an RGB image. |
|
Load an image file and return a numpy array. |
|
Load a video file and return a numpy array of frames. |
|
Convert matplotlib figure to numpy array. |
|
Preprocesses images for saving to GIF or MP4. |
|
Saves a sequence of images to a GIF file. |
|
Saves a sequence of images to an MP4 file. |
|
Saves a sequence of images to a video file. |
|
Traverse a directory tree and yield file paths matching specified file types. |
- zea.io_lib.compute_global_palette_by_histogram(pillow_imgs, bits_per_channel=5, palette_size=256)[source]ΒΆ
Computes a global color palette for a sequence of images using histogram analysis.
- Parameters:
pillow_imgs (list) β List of pillow images. All images should be in RGB mode.
bits_per_channel (int, optional) β Number of bits to use per color channel for histogram binning. Can take values between 1 and 7. Defaults to 5.
palette_size (int, optional) β Number of colors in the resulting palette. Defaults to 256.
- Returns:
A PIL βPβ mode image containing the computed color palette.
- Return type:
PIL.Image
- Raises:
ValueError β If bits_per_channel or palette_size is outside of range.
- zea.io_lib.grayscale_to_rgb(image)[source]ΒΆ
Converts a grayscale image to an RGB image.
- Parameters:
image (ndarray) β Grayscale image. Must have shape (height, width).
- Returns:
RGB image.
- Return type:
ndarray
- zea.io_lib.load_image(filename, mode='L')[source]ΒΆ
Load an image file and return a numpy array.
Supported file types: jpg, png.
- Parameters:
filename (str) β The path to the image file.
mode (str, optional) β Color mode: βLβ (grayscale) or βRGBβ. Defaults to βLβ.
- Returns:
A numpy array of the image.
- Return type:
numpy.ndarray
- Raises:
ValueError β If the file extension or mode is not supported.
- zea.io_lib.load_video(filename, mode='L')[source]ΒΆ
Load a video file and return a numpy array of frames.
Supported file types: mp4, gif.
- Parameters:
filename (str) β The path to the video file.
mode (str, optional) β Color mode: βLβ (grayscale) or βRGBβ. Defaults to βLβ.
- Returns:
Array of frames (num_frames, H, W) or (num_frames, H, W, C)
- Return type:
numpy.ndarray
- Raises:
ValueError β If the file extension or mode is not supported.
- zea.io_lib.matplotlib_figure_to_numpy(fig, **kwargs)[source]ΒΆ
Convert matplotlib figure to numpy array.
- Parameters:
fig (matplotlib.figure.Figure) β figure to convert.
- Returns:
numpy array of figure.
- Return type:
np.ndarray
- zea.io_lib.preprocess_for_saving(images)[source]ΒΆ
Preprocesses images for saving to GIF or MP4.
- Parameters:
images (ndarray, list[ndarray]) β Images. Must have shape (n_frames, height, width, channels) or (n_frames, height, width).
- zea.io_lib.save_to_gif(images, filename, fps=20, shared_color_palette=True)[source]ΒΆ
Saves a sequence of images to a GIF file.
Note
Itβs recommended to use
save_video()for a more general interface that supports multiple formats.- Parameters:
images (list or np.ndarray) β List or array of images. Must have shape (n_frames, height, width, channels) or (n_frames, height, width). If channel axis is not present, or is 1, grayscale image is assumed, which is then converted to RGB. Images should be uint8.
filename (str or Path) β Filename to which data should be written.
fps (int) β Frames per second of rendered format.
shared_color_palette (bool, optional) β If True, creates a global color palette across all frames, ensuring consistent colors throughout the GIF. Defaults to True, which is default behavior of PIL.Image.save. Note: True increases speed and shrinks file size for longer sequences.
- zea.io_lib.save_to_mp4(images, filename, fps=20, shared_color_palette=False)[source]ΒΆ
Saves a sequence of images to an MP4 file.
Note
Itβs recommended to use
save_video()for a more general interface that supports multiple formats.- Parameters:
images (list or np.ndarray) β List or array of images. Must have shape (n_frames, height, width, channels) or (n_frames, height, width). If channel axis is not present, or is 1, grayscale image is assumed, which is then converted to RGB. Images should be uint8.
filename (str or Path) β Filename to which data should be written.
fps (int) β Frames per second of rendered format.
shared_color_palette (bool, optional) β If True, creates a global color palette across all frames, ensuring consistent colors throughout the MP4. Note: True can cause slow saving for longer sequences.
- Raises:
ImportError β If imageio-ffmpeg is not installed.
- Returns:
Success message.
- Return type:
str
- zea.io_lib.save_video(images, filename, fps=20, **kwargs)[source]ΒΆ
Saves a sequence of images to a video file.
Supported file types: mp4, gif.
- Parameters:
images (list or np.ndarray) β List or array of images. Must have shape (n_frames, height, width, channels) or (n_frames, height, width). If channel axis is not present, or is 1, grayscale image is assumed, which is then converted to RGB. Images should be uint8.
filename (str or Path) β Filename to which data should be written.
fps (int) β Frames per second of rendered format.
**kwargs β Additional keyword arguments passed to the specific save function. For GIF and mp4 files, this includes shared_color_palette (bool).
- Raises:
ValueError β If the file extension is not supported.
- zea.io_lib.search_file_tree(directory, filetypes=None, verbose=True, relative=False)[source]ΒΆ
Traverse a directory tree and yield file paths matching specified file types.
- Parameters:
directory (str or Path) β The root directory to start the search.
filetypes (list of str, optional) β List of file extensions to match. If None, file types supported by zea are matched. Defaults to None.
verbose (bool, optional) β If True, logs the search process. Defaults to True.
relative (bool, optional) β If True, yields file paths relative to the root directory. Defaults to False.
- Yields:
Path β Paths of files matching the specified file types.