File size: 1,172 Bytes
f5ea9be 7fe64df f5ea9be f4395f1 f5ea9be |
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 |
import gradio as gr
from PIL import Image
import io
def compress_and_resize_image(image_path, max_width, max_height):
# Open the image
img = Image.open(image_path)
# Preserve aspect ratio while resizing
img.thumbnail((max_width, max_height)) # This keeps aspect ratio intact
# Compress the image
compressed_io = io.BytesIO()
img.save(compressed_io, format='JPEG', quality=80)
compressed_io.seek(0)
# Save compressed image
compressed_filename = "compressed_resized_image.jpg"
with open(compressed_filename, "wb") as f:
f.write(compressed_io.read())
return compressed_filename
# Gradio interface
interface = gr.Interface(
fn=compress_and_resize_image,
inputs=[
gr.Image(type="filepath", label="Upload Image"),
gr.Number(label="Max Width", value=800),
gr.Number(label="Max Height", value=800)
],
outputs=gr.File(label="Download Compressed & Resized Image"),
title="BG CoRe (Co.mpress Re.size) Image Utility",
description="Upload an image, set max width or height to resize while preserving aspect ratio, then download the compressed version."
)
interface.launch() |