%%capture
!pip install kornia
!pip install kornia-rs
Resize anti-alias
Basic
Rescale
kornia.geometry
In this tutorial we are going to learn how to resize an image with anti-alias.
Install Kornia
Prepare the data
Download an example image
import io
import requests
def download_image(url: str, filename: str = "") -> str:
= url.split("/")[-1] if len(filename) == 0 else filename
filename # Download
= io.BytesIO(requests.get(url).content)
bytesio # Save file
with open(filename, "wb") as outfile:
outfile.write(bytesio.getbuffer())
return filename
= "https://github.com/kornia/data/raw/main/drslump.jpg"
url download_image(url)
'drslump.jpg'
import kornia as K
import torch
from matplotlib import pyplot as plt
def imshow(input: torch.Tensor):
= input.shape[0]
B = plt.subplots(ncols=B, nrows=1, figsize=(20, 10))
fig, axes = axes if B > 1 else [axes]
axes for idx, ax in enumerate(axes):
input[idx]))
ax.imshow(K.utils.tensor_to_image("off")
ax.axis(
= K.io.load_image("drslump.jpg", K.io.ImageLoadType.RGB32)[None, ...] # BxCxHxW
data: torch.Tensor
# plot
imshow(data)
Plain resize vs Antializased resize
= K.geometry.rescale(data, (0.125, 0.125))
x_025: torch.Tensor = K.geometry.rescale(data, (0.125, 0.125), antialias=True)
x_025AA: torch.Tensor = torch.stack([x_025, x_025AA], dim=0)
out imshow(out)