rbughao commited on
Commit
f5ea9be
·
verified ·
1 Parent(s): 4f029c0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import io
4
+
5
+ def compress_and_resize_image(image_path, max_width, max_height):
6
+ # Open the image
7
+ img = Image.open(image_path)
8
+
9
+ # Preserve aspect ratio while resizing
10
+ img.thumbnail((max_width, max_height)) # This keeps aspect ratio intact
11
+
12
+ # Compress the image
13
+ compressed_io = io.BytesIO()
14
+ img.save(compressed_io, format='JPEG', quality=30)
15
+ compressed_io.seek(0)
16
+
17
+ # Save compressed image
18
+ compressed_filename = "compressed_resized_image.jpg"
19
+ with open(compressed_filename, "wb") as f:
20
+ f.write(compressed_io.read())
21
+
22
+ return compressed_filename
23
+
24
+ # Gradio interface
25
+ interface = gr.Interface(
26
+ fn=compress_and_resize_image,
27
+ inputs=[
28
+ gr.Image(type="filepath", label="Upload Image"),
29
+ gr.Number(label="Max Width", value=800),
30
+ gr.Number(label="Max Height", value=800)
31
+ ],
32
+ outputs=gr.File(label="Download Compressed & Resized Image"),
33
+ title="Image Compressor & Resizer",
34
+ description="Upload an image, set max width and height to resize while preserving aspect ratio, then download the compressed version."
35
+ )
36
+
37
+ interface.launch()