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 (HxWxC).

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 (HxWxC).

    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_tensor

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

Parameters:

Name Type Description Default
path str

The path of the image file.

required

Returns:

Type Description
Image

The RGB image as a torch tensor (CxHxW).

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_tensor(path: str) -> tv_tensors.Image:
    """Reads and loads the image from a file path as a RGB torch tensor.

    Args:
        path: The path of the image file.

    Returns:
        The RGB image as a torch tensor (CxHxW).

    Raises:
        FileExistsError: If the path does not exist or it is unreachable.
        IOError: If the image could not be loaded.
    """
    image_array = read_image(path)
    return functional.to_image(image_array)

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, dtype=np.uint8)

eva.vision.utils.io.nifti

NIfTI I/O related functions.

read_nifti

Reads and loads a NIfTI image from a file path.

Parameters:

Name Type Description Default
path str

The path to the NIfTI file.

required
slice_index int | None

Whether to read only a slice from the file.

None
use_storage_dtype bool

Whether to cast the raw image array to the inferred type.

True

Returns:

Type Description
NDArray[Any]

The image as a numpy array (height, width, channels).

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(
    path: str, slice_index: int | None = None, *, use_storage_dtype: bool = True
) -> npt.NDArray[Any]:
    """Reads and loads a NIfTI image from a file path.

    Args:
        path: The path to the NIfTI file.
        slice_index: Whether to read only a slice from the file.
        use_storage_dtype: Whether to cast the raw image
            array to the inferred type.

    Returns:
        The image as a numpy array (height, width, channels).

    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
    if slice_index is not None:
        image_data = image_data.slicer[:, :, slice_index : slice_index + 1]  # type: ignore

    image_array = image_data.get_fdata()  # type: ignore
    if use_storage_dtype:
        image_array = image_array.astype(image_data.get_data_dtype())  # type: ignore

    return image_array

save_array_as_nifti

Saved a numpy array as a NIfTI image file.

Parameters:

Name Type Description Default
array ArrayLike

The image array to save.

required
filename str

The name to save the image like.

required
dtype DTypeLike | None

The data type to save the image.

int64
Source code in src/eva/vision/utils/io/nifti.py
def save_array_as_nifti(
    array: npt.ArrayLike,
    filename: str,
    *,
    dtype: npt.DTypeLike | None = np.int64,
) -> None:
    """Saved a numpy array as a NIfTI image file.

    Args:
        array: The image array to save.
        filename: The name to save the image like.
        dtype: The data type to save the image.
    """
    nifti_image = nib.Nifti1Image(array, affine=np.eye(4), dtype=dtype)  # type: ignore
    nifti_image.header.get_xyzt_units()
    nifti_image.to_filename(filename)

fetch_nifti_shape

Fetches the NIfTI image shape from a file.

Parameters:

Name Type Description Default
path str

The path to the NIfTI file.

required

Returns:

Type Description
Tuple[int]

The image shape.

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_nifti_shape(path: str) -> Tuple[int]:
    """Fetches the NIfTI image shape from a file.

    Args:
        path: The path to the NIfTI file.

    Returns:
        The image shape.

    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
    return image.header.get_data_shape()  # type: ignore