File size: 2,594 Bytes
0db0341
51c8f60
0db0341
35a7f8f
0db0341
51c8f60
 
4c45b50
95fc1cb
51c8f60
35a7f8f
51c8f60
 
 
 
 
216e8c5
51c8f60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0db0341
bd00297
0db0341
51c8f60
35a7f8f
 
51c8f60
 
 
 
 
35a7f8f
51c8f60
 
95fc1cb
51c8f60
 
35a7f8f
51c8f60
0db0341
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
from PIL import Image, ImageOps
import os
import tempfile

def optimize_image(image):
    # Leer la imagen original
    img = Image.open(image)
    
    # Ruta temporal para guardar las im谩genes optimizadas
    with tempfile.TemporaryDirectory() as tmpdirname:
        # 1. Compresi贸n sin p茅rdida
        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  # tama帽o en KB
        lossless_img = Image.open(lossless_output_path)
        
        # 2. Compresi贸n con p茅rdida (reducci贸n de calidad)
        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  # tama帽o en KB
        lossy_img = Image.open(lossy_output_path)
        
        # 3. Reducci贸n de resoluci贸n
        reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
        reduced_img = img.resize((img.width // 2, img.height // 2), Image.ANTIALIAS)
        reduced_img.save(reduced_output_path, format="JPEG", quality=85, optimize=True)
        reduced_size = os.path.getsize(reduced_output_path) / 1024  # tama帽o en KB
        reduced_img = Image.open(reduced_output_path)
        
        # Retornar im谩genes optimizadas y sus tama帽os
        return (lossless_img, f"Sin p茅rdida: {lossless_size:.2f} KB",
                lossy_img, f"Con p茅rdida: {lossy_size:.2f} KB",
                reduced_img, f"Reducci贸n de resoluci贸n: {reduced_size:.2f} KB")

# Interfaz de Gradio
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")

    # Mostrar las im谩genes optimizadas y sus tama帽os
    optimized_output1 = gr.Image(label="Optimizaci贸n sin p茅rdida")
    optimized_size1 = gr.Text(label="Tama帽o del archivo sin p茅rdida")

    optimized_output2 = gr.Image(label="Optimizaci贸n con p茅rdida")
    optimized_size2 = gr.Text(label="Tama帽o del archivo con p茅rdida")

    optimized_output3 = gr.Image(label="Reducci贸n de resoluci贸n")
    optimized_size3 = gr.Text(label="Tama帽o del archivo con reducci贸n de resoluci贸n")

    optimize_button.click(fn=optimize_image, inputs=image_input, outputs=[optimized_output1, optimized_size1, optimized_output2, optimized_size2, optimized_output3, optimized_size3])

demo.launch()