File size: 5,349 Bytes
0db0341
4dc3d40
0db0341
35a7f8f
0db0341
1ce0886
4c45b50
95fc1cb
3ef5903
 
35a7f8f
1ce0886
51c8f60
1ce0886
e3671eb
3ef5903
fc6a1b5
216e8c5
2ba1410
51c8f60
1ce0886
e3671eb
3ef5903
fc6a1b5
51c8f60
1ce0886
51c8f60
1ce0886
 
 
e3671eb
3ef5903
fc6a1b5
4dc3d40
2ba1410
4dc3d40
1ce0886
e3671eb
3ef5903
fc6a1b5
51c8f60
2ba1410
e3671eb
fc6a1b5
 
 
 
e3671eb
0db0341
2ba1410
1fa88c0
2ba1410
0db0341
51c8f60
35a7f8f
 
51c8f60
 
e3671eb
bbe07c9
 
1ce0886
1fa88c0
bbe07c9
1fa88c0
2ba1410
bbe07c9
1ce0886
 
1fa88c0
bbe07c9
1fa88c0
95fc1cb
bbe07c9
1ce0886
 
1fa88c0
bbe07c9
1fa88c0
35a7f8f
bbe07c9
 
1ce0886
1fa88c0
bbe07c9
1fa88c0
4dc3d40
e3671eb
 
1ce0886
e3671eb
1fa88c0
 
 
 
e3671eb
 
0db0341
1fa88c0
 
 
 
bbe07c9
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import gradio as gr
from PIL import Image
import os
import tempfile

def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_quality):
    img = Image.open(image)
    
    original_size = os.path.getsize(image.name) / 1024  # tama帽o en KB
    
    with tempfile.TemporaryDirectory() as tmpdirname:
        # 1. Compresi贸n sin p茅rdida (PNG)
        lossless_output_path = os.path.join(tmpdirname, "lossless.png")
        img.save(lossless_output_path, format="PNG", optimize=png_optimize)
        lossless_size = os.path.getsize(lossless_output_path) / 1024
        lossless_diff = original_size - lossless_size
        lossless_img = Image.open(lossless_output_path)
        
        # 2. Compresi贸n con p茅rdida (JPEG)
        lossy_output_path = os.path.join(tmpdirname, "lossy.jpg")
        img.save(lossy_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
        lossy_size = os.path.getsize(lossy_output_path) / 1024
        lossy_diff = original_size - lossy_size
        lossy_img = Image.open(lossy_output_path)
        
        # 3. Reducci贸n de resoluci贸n (JPEG)
        reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
        new_resolution = (img.width * jpeg_resolution // 100, img.height * jpeg_resolution // 100)
        reduced_img = img.resize(new_resolution, Image.LANCZOS)
        reduced_img.save(reduced_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
        reduced_size = os.path.getsize(reduced_output_path) / 1024
        reduced_diff = original_size - reduced_size
        reduced_img = Image.open(reduced_output_path)

        # 4. Compresi贸n con WebP (con p茅rdida)
        webp_lossy_output_path = os.path.join(tmpdirname, "lossy.webp")
        img.save(webp_lossy_output_path, format="WEBP", quality=webp_quality, optimize=True)
        webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024
        webp_lossy_diff = original_size - webp_lossy_size
        webp_lossy_img = Image.open(webp_lossy_output_path)
        
        # Retornar las im谩genes optimizadas, tama帽os y rutas para descarga
        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
        ]

def download_image(file_path):
    return gr.File.update(value=file_path, label="Descargar", visible=True)

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")
            png_optimize = gr.Checkbox(label="Optimizar PNG", value=True)
            optimized_size1 = gr.Text(value="", interactive=False, show_label=False)
            download_button1 = gr.Button("Descargar")
            download_file1 = gr.File(visible=False)

        with gr.Column():
            optimized_output2 = gr.Image(label="Optimizaci贸n con p茅rdida (JPEG)")
            jpeg_quality = gr.Slider(label="Calidad JPEG", minimum=10, maximum=100, value=50, step=1)
            optimized_size2 = gr.Text(value="", interactive=False, show_label=False)
            download_button2 = gr.Button("Descargar")
            download_file2 = gr.File(visible=False)

        with gr.Column():
            optimized_output3 = gr.Image(label="Reducci贸n de resoluci贸n (JPEG)")
            jpeg_resolution = gr.Slider(label="Resoluci贸n JPEG (%)", minimum=10, maximum=100, value=50, step=1)
            optimized_size3 = gr.Text(value="", interactive=False, show_label=False)
            download_button3 = gr.Button("Descargar")
            download_file3 = gr.File(visible=False)

        with gr.Column():
            optimized_output4 = gr.Image(label="Optimizaci贸n WebP con p茅rdida")
            webp_quality = gr.Slider(label="Calidad WebP", minimum=10, maximum=100, value=50, step=1)
            optimized_size4 = gr.Text(value="", interactive=False, show_label=False)
            download_button4 = gr.Button("Descargar")
            download_file4 = gr.File(visible=False)

    optimize_button.click(
        fn=optimize_image,
        inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
        outputs=[
            optimized_output1, optimized_size1, download_file1,
            optimized_output2, optimized_size2, download_file2,
            optimized_output3, optimized_size3, download_file3,
            optimized_output4, optimized_size4, download_file4
        ]
    )

    download_button1.click(download_image, inputs=download_file1, outputs=download_file1)
    download_button2.click(download_image, inputs=download_file2, outputs=download_file2)
    download_button3.click(download_image, inputs=download_file3, outputs=download_file3)
    download_button4.click(download_image, inputs=download_file4, outputs=download_file4)

demo.launch()