File size: 2,051 Bytes
19a285e
 
495efc8
0e6d21b
 
19a285e
0e6d21b
 
 
 
 
19a285e
0e6d21b
 
495efc8
19a285e
0e6d21b
495efc8
19a285e
0e6d21b
 
 
 
 
19a285e
0e6d21b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19a285e
0e6d21b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import kornia as K
from kornia.core import concatenate, Tensor
from PIL import Image
import numpy as np

def rescale_aa(image, height, width):
    # Convert PIL Image to Tensor
    img_np = np.array(image)
    img: Tensor = K.utils.image_to_tensor(img_np).float() / 255.0
    img = img.unsqueeze(0)  # Add batch dimension
    
    img_rescale: Tensor = K.geometry.resize(img, (int(img.shape[2]*height), int(img.shape[3]*width)), antialias=False)
    img_rescale_aa: Tensor = K.geometry.resize(img, (int(img.shape[2]*height), int(img.shape[3]*width)), antialias=True)
    img_out = concatenate([img_rescale, img_rescale_aa], -1)
    
    # Clip the output values from 0 to 1
    return K.utils.tensor_to_image(img_out.clamp_(0, 1))

title = "Kornia Resizing Demo"
description = """
This demo shows the difference between resizing with and without antialiasing using Kornia.
The left half of the output image is resized without antialiasing, and the right half is resized with antialiasing.
"""

with gr.Blocks(title=title) as demo:
    gr.Markdown(f"# {title}")
    gr.Markdown(description)
    
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="pil", label="Input Image")
            height_slider = gr.Slider(minimum=0.005, maximum=2, step=0.005, value=0.25, label="Height Scale")
            width_slider = gr.Slider(minimum=0.005, maximum=2, step=0.005, value=0.25, label="Width Scale")
        
        image_output = gr.Image(type="pil", label="Resized Image")
    
    rescale_button = gr.Button("Rescale")
    rescale_button.click(
        fn=rescale_aa,
        inputs=[image_input, height_slider, width_slider],
        outputs=image_output
    )
    
    gr.Examples(
        examples=[
            ["examples/a.png", 0.25, 0.25],
            ["examples/iron_man.jpeg", 0.25, 0.25],
        ],
        inputs=[image_input, height_slider, width_slider],
        outputs=image_output,
        fn=rescale_aa,
        cache_examples=True
    )

if __name__ == "__main__":
    demo.launch()