mischeiwiller commited on
Commit
0e6d21b
1 Parent(s): 45aa8b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -26
app.py CHANGED
@@ -1,37 +1,57 @@
1
  import gradio as gr
2
-
3
  import kornia as K
4
  from kornia.core import concatenate, Tensor
 
 
5
 
6
- def rescale_aa(file, height, width):
 
 
 
 
7
 
8
- img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
9
- img = img[None]
10
-
11
- img_rescale: Tensor = K.geometry.rescale(img, (float(height),float(width)),antialias=False)
12
- img_rescale_aa: Tensor = K.geometry.rescale(img, (float(height),float(width)),antialias=True)
13
  img_out = concatenate([img_rescale, img_rescale_aa], -1)
14
 
15
- # when antialiasing , some values are going greater than 1 i.e 1.00001 which is giving error while displaying the output image,so clipping the output values from 0 to 1
16
  return K.utils.tensor_to_image(img_out.clamp_(0, 1))
17
 
18
- examples = [
19
- ["examples/a.png",0.25,0.25],
20
- ["examples/iron_man.jpeg",0.25,0.25],
21
- ]
 
22
 
23
- kornia_resizing_demo = gr.Interface(
24
- rescale_aa,
25
- [
26
- gr.inputs.Image(type="file"),
27
- gr.inputs.Slider(minimum=0.005, maximum=2, step=0.005, default=0.25, label="Height"),
28
- gr.inputs.Slider(minimum=0.005, maximum=2, step=0.005, default=0.25, label="Width")
29
- ],
30
- "image",
31
- examples=examples,
32
- live=False,
33
- enable_queue = True,
34
- allow_flagging = "never"
35
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- kornia_resizing_demo.launch()
 
 
1
  import gradio as gr
 
2
  import kornia as K
3
  from kornia.core import concatenate, Tensor
4
+ from PIL import Image
5
+ import numpy as np
6
 
7
+ def rescale_aa(image, height, width):
8
+ # Convert PIL Image to Tensor
9
+ img_np = np.array(image)
10
+ img: Tensor = K.utils.image_to_tensor(img_np).float() / 255.0
11
+ img = img.unsqueeze(0) # Add batch dimension
12
 
13
+ img_rescale: Tensor = K.geometry.resize(img, (int(img.shape[2]*height), int(img.shape[3]*width)), antialias=False)
14
+ img_rescale_aa: Tensor = K.geometry.resize(img, (int(img.shape[2]*height), int(img.shape[3]*width)), antialias=True)
 
 
 
15
  img_out = concatenate([img_rescale, img_rescale_aa], -1)
16
 
17
+ # Clip the output values from 0 to 1
18
  return K.utils.tensor_to_image(img_out.clamp_(0, 1))
19
 
20
+ title = "Kornia Resizing Demo"
21
+ description = """
22
+ This demo shows the difference between resizing with and without antialiasing using Kornia.
23
+ The left half of the output image is resized without antialiasing, and the right half is resized with antialiasing.
24
+ """
25
 
26
+ with gr.Blocks(title=title) as demo:
27
+ gr.Markdown(f"# {title}")
28
+ gr.Markdown(description)
29
+
30
+ with gr.Row():
31
+ with gr.Column():
32
+ image_input = gr.Image(type="pil", label="Input Image")
33
+ height_slider = gr.Slider(minimum=0.005, maximum=2, step=0.005, value=0.25, label="Height Scale")
34
+ width_slider = gr.Slider(minimum=0.005, maximum=2, step=0.005, value=0.25, label="Width Scale")
35
+
36
+ image_output = gr.Image(type="pil", label="Resized Image")
37
+
38
+ rescale_button = gr.Button("Rescale")
39
+ rescale_button.click(
40
+ fn=rescale_aa,
41
+ inputs=[image_input, height_slider, width_slider],
42
+ outputs=image_output
43
+ )
44
+
45
+ gr.Examples(
46
+ examples=[
47
+ ["examples/a.png", 0.25, 0.25],
48
+ ["examples/iron_man.jpeg", 0.25, 0.25],
49
+ ],
50
+ inputs=[image_input, height_slider, width_slider],
51
+ outputs=image_output,
52
+ fn=rescale_aa,
53
+ cache_examples=True
54
+ )
55
 
56
+ if __name__ == "__main__":
57
+ demo.launch()