Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 9207d3a

Browse files
[feat] add image upload tutorial with resize functionality using sliders
1 parent 1782231 commit 9207d3a

File tree

1 file changed

+39
-0
lines changed
  • 6_upload_an_image_in_gradio

1 file changed

+39
-0
lines changed

6_upload_an_image_in_gradio/app.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
app.py - A Gradio application to upload and resize an image.
3+
4+
This application allows users to upload an image and specify the desired width and height for resizing. The resized image is then displayed as output.
5+
"""
6+
7+
import gradio as gr
8+
9+
# Function to resize an image
10+
def resize_image(image, width, height):
11+
"""
12+
Resize the uploaded image to the specified width and height.
13+
14+
Args:
15+
image (PIL.Image): The uploaded image.
16+
width (int): The desired width of the resized image.
17+
height (int): The desired height of the resized image.
18+
19+
Returns:
20+
PIL.Image: The resized image.
21+
"""
22+
if image is None:
23+
return None
24+
return image.resize((width, height))
25+
26+
# Create a Gradio interface for resizing images
27+
interface = gr.Interface(
28+
fn=resize_image,
29+
inputs=[
30+
gr.Image(type="pil", label="Upload Image"),
31+
gr.Slider(minimum=50, maximum=1000, value=300, step=10, label="Width"),
32+
gr.Slider(minimum=50, maximum=1000, value=300, step=10, label="Height")
33+
],
34+
outputs=gr.Image(type="pil", label="Resized Image")
35+
)
36+
37+
if __name__ == "__main__":
38+
# Launch the Gradio interface
39+
interface.launch()

0 commit comments

Comments
 (0)