def apply_colormap(image: Image.Image) -> Image.Image:
    """Apply a colormap to a single-channel PIL Image using torch and PIL.

    This function converts a grayscale image to a colored image using the 'jet' colormap.
    The colormap is created by interpolating between 9 key colors from dark blue to dark
    red.

    Args:
        image (``Image.Image``): A single-channel PIL Image or an object that can be
            converted to PIL Image. If not already in 'L' mode (8-bit grayscale), it will
            be converted.

    Returns:
        ``Image.Image``: A new PIL Image in RGB mode with the colormap applied.

    Raises:
        TypeError: If the input cannot be converted to a PIL Image.

    Example:
        Create a random grayscale image and apply colormap:

        >>> from PIL import Image
        >>> import numpy as np
        >>> # Create a sample grayscale image
        >>> gray = np.random.randint(0, 256, (100, 100), dtype=np.uint8)
        >>> gray_image = Image.fromarray(gray, mode='L')
        >>> # Apply the jet colormap
        >>> colored_image = apply_colormap(gray_image)
        >>> colored_image.mode
        'RGB'

        Apply to non-PIL input:

        >>> # NumPy array input is automatically converted
        >>> colored_image = apply_colormap(gray)
        >>> isinstance(colored_image, Image.Image)
        True

        Invalid input raises TypeError:

        >>> apply_colormap("not an image")  # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
            ...
        TypeError: Input must be a PIL Image object or an object that can be...

    Note:
        - Input is automatically converted to grayscale if not already
        - Uses a custom 'jet' colormap interpolated between 9 key colors
        - Output is always in RGB mode regardless of input mode
        - The colormap interpolation uses bilinear mode for smooth transitions
    """
    # Try to convert the input to a PIL Image if it's not already
    if not isinstance(image, Image.Image):
        try:
            image = Image.fromarray(image)
        except TypeError:
            msg = "Input must be a PIL Image object or an object that can be converted to PIL Image"
            raise TypeError(msg) from None

    # Ensure image is in 'L' mode (8-bit pixels, black and white)
    if image.mode != "L":
        image = image.convert("L")

    # Define colormap values for the 'jet' colormap
    colormap_values = [
        (0, 0, 143),  # Dark blue
        (0, 0, 255),  # Blue
        (0, 127, 255),  # Light blue
        (0, 255, 255),  # Cyan
        (127, 255, 127),  # Light green
        (255, 255, 0),  # Yellow
        (255, 127, 0),  # Orange
        (255, 0, 0),  # Red
        (127, 0, 0),  # Dark red
    ]

    # Create a linear interpolation of the colormap
    colormap_tensor = torch.tensor(colormap_values, dtype=torch.float32)
    colormap_tensor = colormap_tensor.unsqueeze(0).unsqueeze(0)  # Add batch and channel dimensions

    # Interpolate to create a smooth 256-color palette
    interpolated = F.interpolate(colormap_tensor, size=(256, 3), mode="bilinear", align_corners=False)
    interpolated = interpolated.squeeze().byte()

    # Convert the interpolated tensor to a flat list for PIL
    palette = interpolated.flatten().tolist()

    # Apply the colormap to the image
    colored_image = image.convert("P")  # Convert to 8-bit pixels, mapped to a palette
    colored_image.putpalette(palette)  # Apply our custom palette
    return colored_image.convert("RGB")  # Convert back to RGB for display
Apply a colormap to a single-channel PIL Image using torch and PIL.

    This function converts a grayscale image to a colored image using the 'jet' colormap.
    The colormap is created by interpolating between 9 key colors from dark blue to dark
    red.

    Args:
        image (``Image.Image``): A single-channel PIL Image or an object that can be
            converted to PIL Image. If not already in 'L' mode (8-bit grayscale), it will
            be converted.

    Returns:
        ``Image.Image``: A new PIL Image in RGB mode with the colormap applied.

    Raises:
        TypeError: If the input cannot be converted to a PIL Image.

    Example:
        Create a random grayscale image and apply colormap:

        >>> from PIL import Image
        >>> import numpy as np
        >>> # Create a sample grayscale image
        >>> gray = np.random.randint(0, 256, (100, 100), dtype=np.uint8)
        >>> gray_image = Image.fromarray(gray, mode='L')
        >>> # Apply the jet colormap
        >>> colored_image = apply_colormap(gray_image)
        >>> colored_image.mode
        'RGB'

        Apply to non-PIL input:

        >>> # NumPy array input is automatically converted
        >>> colored_image = apply_colormap(gray)
        >>> isinstance(colored_image, Image.Image)
        True

        Invalid input raises TypeError:

        >>> apply_colormap("not an image")  # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
            ...
        TypeError: Input must be a PIL Image object or an object that can be...

    Note:
        - Input is automatically converted to grayscale if not already
        - Uses a custom 'jet' colormap interpolated between 9 key colors
        - Output is always in RGB mode regardless of input mode
        - The colormap interpolation uses bilinear mode for smooth transitions
    
src/anomalib/visualization/image/functional.py