import gradio as gr from PIL import Image import os import tempfile def optimize_image(image): img = Image.open(image) original_size = os.path.getsize(image.name) / 1024 # tamaño en KB with tempfile.TemporaryDirectory() as tmpdirname: lossless_output_path = os.path.join(tmpdirname, "lossless.png") img.save(lossless_output_path, format="PNG", optimize=True) lossless_size = os.path.getsize(lossless_output_path) / 1024 lossless_img = Image.open(lossless_output_path) lossless_diff = original_size - lossless_size lossy_output_path = os.path.join(tmpdirname, "lossy.jpg") img.save(lossy_output_path, format="JPEG", quality=50, optimize=True) lossy_size = os.path.getsize(lossy_output_path) / 1024 lossy_img = Image.open(lossy_output_path) lossy_diff = original_size - lossy_size reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg") reduced_img = img.resize((img.width // 2, img.height // 2), Image.LANCZOS) reduced_img.save(reduced_output_path, format="JPEG", quality=85, optimize=True) reduced_size = os.path.getsize(reduced_output_path) / 1024 reduced_img = Image.open(reduced_output_path) reduced_diff = original_size - reduced_size webp_lossy_output_path = os.path.join(tmpdirname, "lossy.webp") img.save(webp_lossy_output_path, format="WEBP", quality=50, optimize=True) webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024 webp_lossy_img = Image.open(webp_lossy_output_path) webp_lossy_diff = original_size - webp_lossy_size return [ lossless_img, f"Sin pérdida: {lossless_size:.2f} KB ({-lossless_diff:.2f} KB)", lossless_output_path, lossy_img, f"Con pérdida: {lossy_size:.2f} KB ({-lossy_diff:.2f} KB)", lossy_output_path, reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB ({-reduced_diff:.2f} KB)", reduced_output_path, webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB ({-webp_lossy_diff:.2f} KB)", webp_lossy_output_path ] with gr.Blocks() as demo: gr.Markdown("### Optimización de imágenes para la web") image_input = gr.File(label="Sube tu imagen", file_types=['image']) optimize_button = gr.Button("Optimizar") with gr.Row(): with gr.Column(): optimized_output1 = gr.Image(label="Optimización sin pérdida") optimized_size1 = gr.Text(visible=False) download_button1 = gr.Button("Descargar") with gr.Column(): optimized_output2 = gr.Image(label="Optimización con pérdida") optimized_size2 = gr.Text(visible=False) download_button2 = gr.Button("Descargar") with gr.Column(): optimized_output3 = gr.Image(label="Reducción de resolución") optimized_size3 = gr.Text(visible=False) download_button3 = gr.Button("Descargar") with gr.Column(): optimized_output4 = gr.Image(label="Optimización WebP con pérdida") optimized_size4 = gr.Text(visible=False) download_button4 = gr.Button("Descargar") optimize_button.click( fn=optimize_image, inputs=image_input, outputs=[ optimized_output1, optimized_size1, download_button1, optimized_output2, optimized_size2, download_button2, optimized_output3, optimized_size3, download_button3, optimized_output4, optimized_size4, download_button4 ] ) download_button1.click(lambda x: x, inputs=optimized_size1, outputs=download_button1) download_button2.click(lambda x: x, inputs=optimized_size2, outputs=download_button2) download_button3.click(lambda x: x, inputs=optimized_size3, outputs=download_button3) download_button4.click(lambda x: x, inputs=optimized_size4, outputs=download_button4) demo.launch()