def visualize_anomaly_map(
    anomaly_map: Image.Image | torch.Tensor,
    colormap: bool = True,
    normalize: bool = False,
) -> Image.Image:
    """Visualize an anomaly map by applying normalization and/or colormap.

    This function takes an anomaly map and converts it to a visualization by optionally
    normalizing the values and applying a colormap. The input can be either a PIL Image
    or PyTorch tensor.

    Args:
        anomaly_map (:class:`PIL.Image.Image` | :class:`torch.Tensor`): Input anomaly
            map to visualize. If a tensor is provided, it will be converted to a PIL
            Image.
        colormap (bool, optional): Whether to apply a colormap to the anomaly map.
            When ``True``, converts the image to a colored heatmap visualization.
            When ``False``, converts to RGB grayscale. Defaults to ``True``.
        normalize (bool, optional): Whether to normalize the anomaly map values to
            [0, 255] range before visualization. When ``True``, linearly scales the
            values using min-max normalization. Defaults to ``False``.

    Returns:
        :class:`PIL.Image.Image`: Visualized anomaly map as a PIL Image in RGB mode.
            If ``colormap=True``, returns a heatmap visualization. Otherwise returns
            a grayscale RGB image.

    Examples:
        Visualize a PIL Image anomaly map:

        >>> from PIL import Image
        >>> import numpy as np
        >>> # Create sample anomaly map
        >>> data = np.random.rand(100, 100).astype(np.float32)
        >>> anomaly_map = Image.fromarray(data, mode='F')
        >>> # Visualize with normalization and colormap
        >>> vis = visualize_anomaly_map(anomaly_map, normalize=True, colormap=True)
        >>> vis.mode
        'RGB'

        Visualize a PyTorch tensor anomaly map:

        >>> import torch
        >>> # Create random tensor
        >>> tensor_map = torch.rand(100, 100)
        >>> # Visualize without normalization
        >>> vis = visualize_anomaly_map(tensor_map, normalize=False, colormap=True)
        >>> isinstance(vis, Image.Image)
        True

    Note:
        - Input tensors are automatically converted to PIL Images
        - The function always returns an RGB mode image
        - When ``normalize=True``, uses min-max normalization to [0, 255] range
        - The colormap used is the default from :func:`apply_colormap`
    """
    image = to_pil_image(anomaly_map) if isinstance(anomaly_map, torch.Tensor) else anomaly_map.copy()

    if normalize:
        # Get the min and max pixel values
        min_value = image.getextrema()[0]
        max_value = image.getextrema()[1]

        # Create a normalized image
        image = image.point(lambda x: (x - min_value) * 255 / (max_value - min_value))

    return apply_colormap(image) if colormap else image.convert("RGB")
Visualize an anomaly map by applying normalization and/or colormap.

    This function takes an anomaly map and converts it to a visualization by optionally
    normalizing the values and applying a colormap. The input can be either a PIL Image
    or PyTorch tensor.

    Args:
        anomaly_map (:class:`PIL.Image.Image` | :class:`torch.Tensor`): Input anomaly
            map to visualize. If a tensor is provided, it will be converted to a PIL
            Image.
        colormap (bool, optional): Whether to apply a colormap to the anomaly map.
            When ``True``, converts the image to a colored heatmap visualization.
            When ``False``, converts to RGB grayscale. Defaults to ``True``.
        normalize (bool, optional): Whether to normalize the anomaly map values to
            [0, 255] range before visualization. When ``True``, linearly scales the
            values using min-max normalization. Defaults to ``False``.

    Returns:
        :class:`PIL.Image.Image`: Visualized anomaly map as a PIL Image in RGB mode.
            If ``colormap=True``, returns a heatmap visualization. Otherwise returns
            a grayscale RGB image.

    Examples:
        Visualize a PIL Image anomaly map:

        >>> from PIL import Image
        >>> import numpy as np
        >>> # Create sample anomaly map
        >>> data = np.random.rand(100, 100).astype(np.float32)
        >>> anomaly_map = Image.fromarray(data, mode='F')
        >>> # Visualize with normalization and colormap
        >>> vis = visualize_anomaly_map(anomaly_map, normalize=True, colormap=True)
        >>> vis.mode
        'RGB'

        Visualize a PyTorch tensor anomaly map:

        >>> import torch
        >>> # Create random tensor
        >>> tensor_map = torch.rand(100, 100)
        >>> # Visualize without normalization
        >>> vis = visualize_anomaly_map(tensor_map, normalize=False, colormap=True)
        >>> isinstance(vis, Image.Image)
        True

    Note:
        - Input tensors are automatically converted to PIL Images
        - The function always returns an RGB mode image
        - When ``normalize=True``, uses min-max normalization to [0, 255] range
        - The colormap used is the default from :func:`apply_colormap`
    
src/anomalib/visualization/image/functional.py