stouputils.data_science.dataset.image_loader module#
This module contains utility functions for loading image data from directories.
It provides alternatives to Keras image loading functions, focused on efficient image loading, resizing, and preprocessing using PIL. The main functionality allows loading images from directories into numpy arrays suitable for machine learning model input.
- ALLOWLIST_FORMATS: tuple[str, ...] = ('.blp', '.bmp', '.dib', '.bufr', '.cur', '.pcx', '.dcx', '.dds', '.ps', '.eps', '.fit', '.fits', '.fli', '.flc', '.ftc', '.ftu', '.gbr', '.gif', '.grib', '.h5', '.hdf', '.png', '.apng', '.jp2', '.j2k', '.jpc', '.jpf', '.jpx', '.j2c', '.icns', '.ico', '.im', '.iim', '.jfif', '.jpe', '.jpg', '.jpeg', '.mpg', '.mpeg', '.tif', '.tiff', '.msp', '.pcd', '.pxr', '.pbm', '.pgm', '.ppm', '.pnm', '.pfm', '.psd', '.qoi', '.bw', '.rgb', '.rgba', '.sgi', '.ras', '.tga', '.icb', '.vda', '.vst', '.webp', '.wmf', '.emf', '.xbm', '.xpm')#
List of image formats supported by PIL
- load_images_from_directory(directory_path: str, image_size: tuple[int, int] = (224, 224), color_mode: str | None = 'RGB', resample: Resampling = Resampling.LANCZOS, to_float32: bool = True, **kwargs: Any) list[tuple[ndarray[Any, dtype[Any]], str]] [source]#
Load images from a directory using PIL instead of Keras.
This function loads all images from a directory and its subdirectories, resizes them to the specified size, converts them to the specified color mode, and returns them as a list of numpy arrays. Unlike Keras’ image_dataset_from_directory, this function doesn’t create batches or labels. If directory_path is a file path, it will load that single image.
- Parameters:
directory_path (str) – Path to the directory containing images or a single image file
image_size (tuple[int, int]) – Size to which images should be resized
color_mode (str | None) – Color mode to use (“RGB” or “grayscale”)
resample (Image.Resampling) – Resampling filter to use when resizing
to_float32 (bool) – Whether to convert the image to float32 (between 0 and 1)
**kwargs (Any) – Additional arguments (ignored, for compatibility)
- Returns:
- List of tuples containing images
with shape (height, width, channels) and their file paths
- Return type:
list[tuple[NDArray[Any], str]]