Loading imgutils/data/image.py +56 −0 Original line number Diff line number Diff line Loading @@ -19,6 +19,27 @@ MultiImagesTyping = Union[ImageTyping, List[ImageTyping], Tuple[ImageTyping, ... def load_image(image: ImageTyping, mode=None, force_background: Optional[str] = 'white'): """ 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. :param image: The source of the image to be loaded. :type image: Union[str, PathLike, bytes, bytearray, BinaryIO, Image.Image] :param mode: The mode to convert the image to. If None, the original mode will be retained. (default: ``None``) :type mode: str or None :param force_background: The color of the background to be added for RGBA images. If None, no background will be added. (default: ``white``) :type force_background: str or None :return: The loaded and transformed image. :rtype: Image.Image """ if isinstance(image, (str, PathLike, bytes, bytearray, BinaryIO)) or _is_readable(image): image = Image.open(image) elif isinstance(image, Image.Image): Loading @@ -36,6 +57,25 @@ def load_image(image: ImageTyping, mode=None, force_background: Optional[str] = def load_images(images: MultiImagesTyping, mode=None, force_background: Optional[str] = 'white') -> List[Image.Image]: """ 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 :func:`load_image` function on each item to load and transform the images. The images are returned as a list of PIL Image objects. :param images: The sources of the images to be loaded. :type images: MultiImagesTyping :param mode: The mode to convert the images to. If None, the original modes will be retained. (default: ``None``) :type mode: str or None :param force_background: The color of the background to be added for RGBA images. If None, no background will be added. (default: ``white``) :type force_background: str or None :return: A list of loaded and transformed images. :rtype: List[Image.Image] """ if not isinstance(images, (list, tuple)): images = [images] Loading @@ -43,5 +83,21 @@ def load_images(images: MultiImagesTyping, mode=None, force_background: Optional def add_background_for_rgba(image: ImageTyping, background: str = 'white'): """ Adds a background color to the RGBA image if it has an alpha channel. The function checks if the provided image is in RGBA format and has an alpha channel. If it does, a background of the specified color will be added to the image using the :func:`imgutils.data.layer.istack` function. The resulting image is then converted to RGB. :param image: The RGBA image to add a background to. :type image: ImageTyping :param background: The color of the background to be added. (default: ``white``) :type background: str :return: The image with the added background, converted to RGB. :rtype: Image.Image """ from .layer import istack return istack(background, image).convert('RGB') Loading
imgutils/data/image.py +56 −0 Original line number Diff line number Diff line Loading @@ -19,6 +19,27 @@ MultiImagesTyping = Union[ImageTyping, List[ImageTyping], Tuple[ImageTyping, ... def load_image(image: ImageTyping, mode=None, force_background: Optional[str] = 'white'): """ 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. :param image: The source of the image to be loaded. :type image: Union[str, PathLike, bytes, bytearray, BinaryIO, Image.Image] :param mode: The mode to convert the image to. If None, the original mode will be retained. (default: ``None``) :type mode: str or None :param force_background: The color of the background to be added for RGBA images. If None, no background will be added. (default: ``white``) :type force_background: str or None :return: The loaded and transformed image. :rtype: Image.Image """ if isinstance(image, (str, PathLike, bytes, bytearray, BinaryIO)) or _is_readable(image): image = Image.open(image) elif isinstance(image, Image.Image): Loading @@ -36,6 +57,25 @@ def load_image(image: ImageTyping, mode=None, force_background: Optional[str] = def load_images(images: MultiImagesTyping, mode=None, force_background: Optional[str] = 'white') -> List[Image.Image]: """ 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 :func:`load_image` function on each item to load and transform the images. The images are returned as a list of PIL Image objects. :param images: The sources of the images to be loaded. :type images: MultiImagesTyping :param mode: The mode to convert the images to. If None, the original modes will be retained. (default: ``None``) :type mode: str or None :param force_background: The color of the background to be added for RGBA images. If None, no background will be added. (default: ``white``) :type force_background: str or None :return: A list of loaded and transformed images. :rtype: List[Image.Image] """ if not isinstance(images, (list, tuple)): images = [images] Loading @@ -43,5 +83,21 @@ def load_images(images: MultiImagesTyping, mode=None, force_background: Optional def add_background_for_rgba(image: ImageTyping, background: str = 'white'): """ Adds a background color to the RGBA image if it has an alpha channel. The function checks if the provided image is in RGBA format and has an alpha channel. If it does, a background of the specified color will be added to the image using the :func:`imgutils.data.layer.istack` function. The resulting image is then converted to RGB. :param image: The RGBA image to add a background to. :type image: ImageTyping :param background: The color of the background to be added. (default: ``white``) :type background: str :return: The image with the added background, converted to RGB. :rtype: Image.Image """ from .layer import istack return istack(background, image).convert('RGB')