def make_grid(tensor: Tensor, n_row: Optional[int] = None, padding: int = 2) -> Tensor:
    """Convert a batched tensor to one image with padding in between.

    Args:
        tensor: A batched tensor of shape (B, C, H, W).
        n_row: Number of images displayed in each row of the grid.
        padding: The amount of padding to add between images.

    Returns:
        Tensor: The combined image grid.

    """
    if not isinstance(tensor, torch.Tensor):
        raise TypeError("Input tensor must be a PyTorch tensor.")

    B, C, H, W = tensor.shape
    if n_row is None:
        n_row = int(torch.sqrt(torch.tensor(B, dtype=torch.float32)).ceil())
    n_col = (B + n_row - 1) // n_row

    # Calculate new dimensions with padding
    padded_H = H + padding
    padded_W = W + padding
    combined_H = n_row * padded_H - padding
    combined_W = n_col * padded_W - padding

    # Initialize an empty canvas with the padding value
    pad_value = 0
    combined_image = torch.full((C, combined_H, combined_W), pad_value, dtype=tensor.dtype)

    for idx in range(B):
        row = idx // n_col
        col = idx % n_col

        top = row * padded_H
        left = col * padded_W

        combined_image[:, top : top + H, left : left + W] = tensor[idx]

    return combined_image
Convert a batched tensor to one image with padding in between.

    Args:
        tensor: A batched tensor of shape (B, C, H, W).
        n_row: Number of images displayed in each row of the grid.
        padding: The amount of padding to add between images.

    Returns:
        Tensor: The combined image grid.

    
kornia/utils/image.py