Skip to content

cover_factory

Convenience function to create covers for the BMZ.

create_cover(directory, array_in, array_out) #

Create a cover image from input and output arrays.

Input and output arrays are expected to be SC(Z)YX. For images with a Z dimension, the middle slice is taken.

Parameters:

Name Type Description Default
directory Path

Directory in which to save the cover.

required
array_in ndarray

Array from which to create the cover image.

required
array_out ndarray

Array from which to create the cover image.

required

Returns:

Type Description
Path

Path to the saved cover image.

Source code in src/careamics/model_io/bioimage/cover_factory.py
def create_cover(directory: Path, array_in: NDArray, array_out: NDArray) -> Path:
    """Create a cover image from input and output arrays.

    Input and output arrays are expected to be SC(Z)YX. For images with a Z
    dimension, the middle slice is taken.

    Parameters
    ----------
    directory : Path
        Directory in which to save the cover.
    array_in : numpy.ndarray
        Array from which to create the cover image.
    array_out : numpy.ndarray
        Array from which to create the cover image.

    Returns
    -------
    Path
        Path to the saved cover image.
    """
    # extract slice and normalize arrays
    slice_in = _get_norm_slice(array_in)
    slice_out = _get_norm_slice(array_out)

    horizontal_split = slice_in.shape[-1] == slice_out.shape[-1]
    if not horizontal_split:
        if slice_in.shape[-2] != slice_out.shape[-2]:
            raise ValueError("Input and output arrays have different shapes.")

    # convert to Image
    image_in = _convert_to_image(array_in.shape, slice_in)
    image_out = _convert_to_image(array_out.shape, slice_out)

    # split horizontally or vertically
    if horizontal_split:
        width = image_in.width // 2

        cover = Image.new("RGB", (image_in.width, image_in.height))
        cover.paste(image_in.crop((0, 0, width, image_in.height)), (0, 0))
        cover.paste(
            image_out.crop(
                (image_in.width - width, 0, image_in.width, image_in.height)
            ),
            (width, 0),
        )
    else:
        height = image_in.height // 2

        cover = Image.new("RGB", (image_in.width, image_in.height))
        cover.paste(image_in.crop((0, 0, image_in.width, height)), (0, 0))
        cover.paste(
            image_out.crop(
                (0, image_in.height - height, image_in.width, image_in.height)
            ),
            (0, height),
        )

    # save
    cover_path = directory / "cover.png"
    cover.save(cover_path)

    return cover_path