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()