def downscale_image(
    image: torch.Tensor,
    size: tuple,
    mode: ImageUpscaleMode = ImageUpscaleMode.PADDING,
) -> torch.Tensor:
    """Downscale image to desired size.

    Args:
        image: Input image tensor
        size: Target size as ``(height, width)``
        mode: Downscaling mode, either ``"padding"`` or ``"interpolation"``

    Returns:
        torch.Tensor: Downscaled image

    Examples:
        >>> x = torch.rand(1, 3, 512, 512)
        >>> y = upscale_image(x, (555, 555), "padding")
        >>> z = downscale_image(y, (512, 512), "padding")
        >>> torch.allclose(x, z)
        True
    """
    input_h, input_w = size
    if mode == ImageUpscaleMode.PADDING:
        image = image[:, :, :input_h, :input_w]
    else:
        image = F.interpolate(input=image, size=(input_h, input_w))

    return image
Downscale image to desired size.

    Args:
        image: Input image tensor
        size: Target size as ``(height, width)``
        mode: Downscaling mode, either ``"padding"`` or ``"interpolation"``

    Returns:
        torch.Tensor: Downscaled image

    Examples:
        >>> x = torch.rand(1, 3, 512, 512)
        >>> y = upscale_image(x, (555, 555), "padding")
        >>> z = downscale_image(y, (512, 512), "padding")
        >>> torch.allclose(x, z)
        True
    
src/anomalib/data/utils/tiler.py