Skip to content

IO

eva.vision.utils.io.image

Image I/O related functions.

read_image

Reads and loads the image from a file path as a RGB.

Parameters:

Name Type Description Default
path str

The path of the image file.

required

Returns:

Type Description
NDArray[uint8]

The RGB image as a numpy array.

Raises:

Type Description
FileExistsError

If the path does not exist or it is unreachable.

IOError

If the image could not be loaded.

Source code in src/eva/vision/utils/io/image.py
def read_image(path: str) -> npt.NDArray[np.uint8]:
    """Reads and loads the image from a file path as a RGB.

    Args:
        path: The path of the image file.

    Returns:
        The RGB image as a numpy array.

    Raises:
        FileExistsError: If the path does not exist or it is unreachable.
        IOError: If the image could not be loaded.
    """
    return read_image_as_array(path, cv2.IMREAD_COLOR)

read_image_as_array

Reads and loads an image file as a numpy array.

Parameters:

Name Type Description Default
path str

The path to the image file.

required
flags int

Specifies the way in which the image should be read.

IMREAD_UNCHANGED

Returns:

Type Description
NDArray[uint8]

The image as a numpy array.

Raises:

Type Description
FileExistsError

If the path does not exist or it is unreachable.

IOError

If the image could not be loaded.

Source code in src/eva/vision/utils/io/image.py
def read_image_as_array(path: str, flags: int = cv2.IMREAD_UNCHANGED) -> npt.NDArray[np.uint8]:
    """Reads and loads an image file as a numpy array.

    Args:
        path: The path to the image file.
        flags: Specifies the way in which the image should be read.

    Returns:
        The image as a numpy array.

    Raises:
        FileExistsError: If the path does not exist or it is unreachable.
        IOError: If the image could not be loaded.
    """
    _utils.check_file(path)
    image = cv2.imread(path, flags=flags)
    if image is None:
        raise IOError(
            f"Input '{path}' could not be loaded. "
            "Please verify that the path is a valid image file."
        )

    if image.ndim == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    if image.ndim == 2 and flags == cv2.IMREAD_COLOR:
        image = image[:, :, np.newaxis]

    return np.asarray(image).astype(np.uint8)

eva.vision.utils.io.nifti

NIfTI I/O related functions.

read_nifti_slice

Reads and loads a NIfTI image from a file path as uint8.

Parameters:

Name Type Description Default
path str

The path to the NIfTI file.

required
slice_index int

The image slice index to return. If None, it will return the full 3D image.

required

Returns:

Type Description
NDArray[Any]

The image as a numpy array.

Raises:

Type Description
FileExistsError

If the path does not exist or it is unreachable.

ValueError

If the input channel is invalid for the image.

Source code in src/eva/vision/utils/io/nifti.py
def read_nifti_slice(path: str, slice_index: int) -> npt.NDArray[Any]:
    """Reads and loads a NIfTI image from a file path as `uint8`.

    Args:
        path: The path to the NIfTI file.
        slice_index: The image slice index to return. If `None`, it will
            return the full 3D image.

    Returns:
        The image as a numpy array.

    Raises:
        FileExistsError: If the path does not exist or it is unreachable.
        ValueError: If the input channel is invalid for the image.
    """
    _utils.check_file(path)
    image_data = nib.load(path)  # type: ignore
    dtype = image_data.get_data_dtype()  # type: ignore
    image_slice = image_data.slicer[:, :, slice_index : slice_index + 1]  # type: ignore
    image_array = image_slice.get_fdata()
    return image_array.astype(dtype)

fetch_total_nifti_slices

Fetches the total slides of a NIfTI image file.

Parameters:

Name Type Description Default
path str

The path to the NIfTI file.

required

Returns:

Type Description
int

The number of the total available slides.

Raises:

Type Description
FileExistsError

If the path does not exist or it is unreachable.

ValueError

If the input channel is invalid for the image.

Source code in src/eva/vision/utils/io/nifti.py
def fetch_total_nifti_slices(path: str) -> int:
    """Fetches the total slides of a NIfTI image file.

    Args:
        path: The path to the NIfTI file.

    Returns:
        The number of the total available slides.

    Raises:
        FileExistsError: If the path does not exist or it is unreachable.
        ValueError: If the input channel is invalid for the image.
    """
    _utils.check_file(path)
    image = nib.load(path)  # type: ignore
    image_shape = image.header.get_data_shape()  # type: ignore
    return image_shape[-1]