Module AmpliVision.src.objs.image.utils

Sub-modules

AmpliVision.src.objs.image.utils.image_loader
AmpliVision.src.objs.image.utils.image_white_balancer

Classes

class ImageLoader

ImageLoader

This class is responsible for loading images from a given folder and converting HEIC images to JPG.

Methods

  • load_images(path_to_imgs: str) -> list
    • This method loads all the images in a folder and returns a list of images.
  • heic2jpg(path_to_heic: str) -> None
    • This method creates .jpg images from the .HEIC images of given folder.

Example

from src.objs.image.utils.image_loader import ImageLoader

images = ImageLoader.load_images('path/to/images')
or 
images = ImageLoader.heic2jpg('path/to/heic')
images = ImageLoader.load_images('path/to/images')
Expand source code
class ImageLoader:
    """
    ## ImageLoader

    This class is responsible for loading images from a given folder and converting HEIC images to JPG.

    ### Methods
    - `load_images(path_to_imgs: str) -> list`
        - This method loads all the images in a folder and returns a list of images.
    - `heic2jpg(path_to_heic: str) -> None`
        - This method creates .jpg images from the .HEIC images of given folder.

    ### Example
    ```python
    from src.objs.image.utils.image_loader import ImageLoader

    images = ImageLoader.load_images('path/to/images')
    or 
    images = ImageLoader.heic2jpg('path/to/heic')
    images = ImageLoader.load_images('path/to/images')
    ```
    """
    @staticmethod
    def load_images(path_to_imgs: str, return_paths_only:bool = False, display: int = 0):
        """
        ### Image loader
        Loads all the images in a folder and returns a list of images

        #### Args:
        path_to_images: path to image folder

        #### Returns:
        List of images
        """

        # acceptable image types
        types = ('.png', '.jpg', 'JPEG')

        # reading single image if path is only one image
        end = path_to_imgs[-4:]

        if end in types:
            return [cv.imread(path_to_imgs)]

        # reading all images of acceptable types from given directory
        imgs = []
        for f_type in types:
            files = [file for file in glob(f"{path_to_imgs}*{f_type}")]
            
            if return_paths_only:
                return files 

            if display:
                for i, f in enumerate(files):
                    name = f[f.rfind('\\') + 1:]
                    print(f"{i} -> {name}")

            imgs.extend([cv.imread(file) for file in files])

        return imgs

    @staticmethod
    def heic2png(path_to_heic: str):
        """
        ### HEIC to PNG converte
        Creates .png images from the .HEIC images of given folder.    

        #### Args:
        path_to_heic: path to image folder

        #### Returns:
        None
        """

        # finding all .HEIC images in the given folder
        # and converting them to .png
        paths = glob(f"{path_to_heic}*.HEIC")
        print(paths)
        for path in paths:
            pillow_heif.register_heif_opener()

            img = im.open(path)
            img.save(path[:-4] + 'png', format="png")
            print(f"{path} converted to PNG")

    @staticmethod
    def heic2jpg(path_to_heic: str):
        """
        ### HEIC to JPG converte
        Creates .jpg images from the .HEIC images of given folder.    

        #### Args:
        path_to_heic: path to image folder

        #### Returns:
        None
        """

        # finding all .HEIC images in the given folder
        # and converting them to .jpg
        paths = glob(f"{path_to_heic}*.HEIC")
        print(paths)
        for path in paths:
            pillow_heif.register_heif_opener()

            img = im.open(path)
            img.save(path[:-4] + 'jpg', format="jpeg")
            print(f"{path} converted to JPG")

Static methods

def heic2jpg(path_to_heic: str)

HEIC to JPG converte

Creates .jpg images from the .HEIC images of given folder.

Args:

path_to_heic: path to image folder

Returns:

None

def heic2png(path_to_heic: str)

HEIC to PNG converte

Creates .png images from the .HEIC images of given folder.

Args:

path_to_heic: path to image folder

Returns:

None

def load_images(path_to_imgs: str, return_paths_only: bool = False, display: int = 0)

Image loader

Loads all the images in a folder and returns a list of images

Args:

path_to_images: path to image folder

Returns:

List of images

class WhiteBalanceAdjuster

WhiteBalanceAdjuster

This class is responsible for adjusting the white balance of an image.

Methods

  • adjust(image: np.ndarray, reference_region: tuple[int, int, int, int] = (62, 80, 20, 20)) -> np.ndarray
    • This method adjusts the white balance of the image.

Example

import cv2 as cv
import numpy as np
from src.objs.image.utils.image_white_balancer import WhiteBalanceAdjuster

scanned_image = cv.imread('path/to/image.jpg')
adjusted_image = WhiteBalanceAdjuster.adjust(scanned_image)
Expand source code
class WhiteBalanceAdjuster:
    """
    # WhiteBalanceAdjuster
    This class is responsible for adjusting the white balance of an image.

    ## Methods
    - `adjust(image: np.ndarray, reference_region: tuple[int, int, int, int] = (62, 80, 20, 20)) -> np.ndarray`
        - This method adjusts the white balance of the image.
    
    ### Example
    ```python
    import cv2 as cv
    import numpy as np
    from src.objs.image.utils.image_white_balancer import WhiteBalanceAdjuster

    scanned_image = cv.imread('path/to/image.jpg')
    adjusted_image = WhiteBalanceAdjuster.adjust(scanned_image)
    ```
    """

    @staticmethod
    def adjust( image: np.ndarray, 
                reference_region: tuple[int, int, int, int] = (62, 80, 20, 20)
            ) -> np.ndarray:
        """
        Adjust the white balance of the image.
        Args:
            image: The image to adjust.
            reference_region: The top-left coordinates and size of the reference region.
        Returns:
            The white-balanced image.
        """

        # Get the top-left coordinates and size of the reference region
        reference_top_left, reference_size = reference_region[:2], reference_region[2:]

        # Create the reference 10x10 square for the reference region for white balancing
        reference_region = image[reference_top_left[1]:reference_top_left[1] + reference_size[1],
                                reference_top_left[0]:reference_top_left[0] + reference_size[0]]

        # Calculate the mean RGB values of the reference region - image white baseline value
        mean_reference = np.mean(reference_region, axis=(0, 1))

        # Scaling factors for each channel
        scale_factors = 255.0 / mean_reference

        # Apply white balancing to the entire image by multiplying the image to the scale factor
        balanced_image = cv.merge([cv.multiply(image[:, :, i], scale_factors[i]) for i in range(3)])

        # Clip the values to the valid range [0, 255]
        balanced_image = np.clip(balanced_image, 0, 255).astype(np.uint8)
        
        return balanced_image

Static methods

def adjust(image: numpy.ndarray, reference_region: tuple[int, int, int, int] = (62, 80, 20, 20)) ‑> numpy.ndarray

Adjust the white balance of the image.

Args

image
The image to adjust.
reference_region
The top-left coordinates and size of the reference region.

Returns

The white-balanced image.