def save_image(filename: Path | str, image: np.ndarray | Figure, root: Path | None = None) -> None:
    """Save image to disk.

    Args:
        filename (Path | str): Output filename
        image (np.ndarray | Figure): Image or matplotlib figure to save
        root (Path | None): Optional root dir to save under. Defaults to None

    Examples:
        >>> img = read_image("input.jpg")
        >>> save_image("output.jpg", img)

        >>> save_image("subdir/output.jpg", img, root=Path("results"))
    """
    if isinstance(image, Figure):
        image = figure_to_array(image)

    file_path = Path(filename)
    # if file_path is absolute, then root is ignored
    # so we remove the top level directory from the path
    if file_path.is_absolute() and root:
        file_path = Path(*file_path.parts[2:])  # OS-AGNOSTIC
    if root:
        file_path = root / file_path

    # Make unique file_path if file already exists
    file_path = duplicate_filename(file_path)

    file_path.parent.mkdir(parents=True, exist_ok=True)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    cv2.imwrite(str(file_path), image)
Save image to disk.

    Args:
        filename (Path | str): Output filename
        image (np.ndarray | Figure): Image or matplotlib figure to save
        root (Path | None): Optional root dir to save under. Defaults to None

    Examples:
        >>> img = read_image("input.jpg")
        >>> save_image("output.jpg", img)

        >>> save_image("subdir/output.jpg", img, root=Path("results"))
    
src/anomalib/data/utils/image.py