|
|
import gradio as gr |
|
|
from PIL import Image |
|
|
import io |
|
|
|
|
|
def compress_and_resize_image(image_path, max_width, max_height): |
|
|
|
|
|
img = Image.open(image_path) |
|
|
|
|
|
|
|
|
img.thumbnail((max_width, max_height)) |
|
|
|
|
|
|
|
|
compressed_io = io.BytesIO() |
|
|
img.save(compressed_io, format='JPEG', quality=80) |
|
|
compressed_io.seek(0) |
|
|
|
|
|
|
|
|
compressed_filename = "compressed_resized_image.jpg" |
|
|
with open(compressed_filename, "wb") as f: |
|
|
f.write(compressed_io.read()) |
|
|
|
|
|
return compressed_filename |
|
|
|
|
|
|
|
|
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() |