imgutils.data.image

This module provides utility functions for image processing and manipulation using the PIL (Python Imaging Library) library.

It includes functions for loading images from various sources, handling multiple images, adding backgrounds to RGBA images, and checking for alpha channels. The module is designed to simplify common image-related tasks in Python applications.

Key features: - Loading images from different sources (file paths, binary data, file-like objects) - Handling multiple images at once - Adding backgrounds to RGBA images - Checking for alpha channels in images

This module is particularly useful for applications that require image preprocessing or manipulation before further processing or analysis.

load_image

imgutils.data.image.load_image(image: str | PathLike | bytes | bytearray | BinaryIO | Image, mode=None, force_background: str | None = 'white')[source]

Loads the image from the provided source and applies necessary transformations.

The function supports loading images from various sources, such as file paths, binary data, or file-like objects. It opens the image using the PIL library and converts it to the specified mode if required. If the image has an RGBA (4-channel) format and a force_background value is provided, a background of the specified color will be added to avoid data anomalies during subsequent conversion processes.

Parameters:
  • image (Union[str, PathLike, bytes, bytearray, BinaryIO, Image.Image]) – The source of the image to be loaded.

  • mode (str or None) – The mode to convert the image to. If None, the original mode will be retained. (default: None)

  • force_background (str or None) – The color of the background to be added for RGBA images. If None, no background will be added. (default: white)

Returns:

The loaded and transformed image.

Return type:

Image.Image

Raises:

TypeError – If the provided image type is not supported.

Example:

>>> from PIL import Image
>>> img = load_image('path/to/image.png', mode='RGB', force_background='white')
>>> isinstance(img, Image.Image)
True
>>> img.mode
'RGB'

load_images

imgutils.data.image.load_images(images: str | PathLike | bytes | bytearray | BinaryIO | Image | List[str | PathLike | bytes | bytearray | BinaryIO | Image] | Tuple[str | PathLike | bytes | bytearray | BinaryIO | Image, ...], mode=None, force_background: str | None = 'white') List[Image][source]

Loads a list of images from the provided sources and applies necessary transformations.

The function takes a single image or a list/tuple of multiple images and calls load_image() function on each item to load and transform the images. The images are returned as a list of PIL Image objects.

Parameters:
  • images (MultiImagesTyping) – The sources of the images to be loaded.

  • mode (str or None) – The mode to convert the images to. If None, the original modes will be retained. (default: None)

  • force_background (str or None) – The color of the background to be added for RGBA images. If None, no background will be added. (default: white)

Returns:

A list of loaded and transformed images.

Return type:

List[Image.Image]

Example:

>>> img_paths = ['path/to/image1.png', 'path/to/image2.jpg']
>>> loaded_images = load_images(img_paths, mode='RGB')
>>> len(loaded_images)
2
>>> all(isinstance(img, Image.Image) for img in loaded_images)
True

has_alpha_channel

imgutils.data.image.has_alpha_channel(image: Image) bool[source]

Determine if the given Pillow image object has an alpha channel (transparency)

Parameters:

image (Image.Image) – Pillow image object

Returns:

Boolean, True if it has an alpha channel, False otherwise

Return type:

bool