def read_mask(path: str | Path, as_tensor: bool = False) -> torch.Tensor | np.ndarray:
    """Read grayscale mask from disk.

    Args:
        path (str | Path): Path to mask file
        as_tensor (bool): If ``True``, return torch.Tensor. Defaults to ``False``

    Returns:
        torch.Tensor | np.ndarray: Mask as tensor or array

    Examples:
        >>> mask = read_mask("mask.png")
        >>> type(mask)
        <class 'numpy.ndarray'>

        >>> mask = read_mask("mask.png", as_tensor=True)
        >>> type(mask)
        <class 'torch.Tensor'>
    """
    image = Image.open(path).convert("L")
    return Mask(to_image(image).squeeze() / 255, dtype=torch.uint8) if as_tensor else np.array(image)
Read grayscale mask from disk.

    Args:
        path (str | Path): Path to mask file
        as_tensor (bool): If ``True``, return torch.Tensor. Defaults to ``False``

    Returns:
        torch.Tensor | np.ndarray: Mask as tensor or array

    Examples:
        >>> mask = read_mask("mask.png")
        >>> type(mask)
        <class 'numpy.ndarray'>

        >>> mask = read_mask("mask.png", as_tensor=True)
        >>> type(mask)
        <class 'torch.Tensor'>
    
src/anomalib/data/utils/image.py