Merlintxu commited on
Commit
2ba1410
1 Parent(s): bbe07c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -9,18 +9,21 @@ def optimize_image(image):
9
  original_size = os.path.getsize(image.name) / 1024 # tamaño en KB
10
 
11
  with tempfile.TemporaryDirectory() as tmpdirname:
 
12
  lossless_output_path = os.path.join(tmpdirname, "lossless.png")
13
  img.save(lossless_output_path, format="PNG", optimize=True)
14
  lossless_size = os.path.getsize(lossless_output_path) / 1024
15
  lossless_img = Image.open(lossless_output_path)
16
  lossless_diff = original_size - lossless_size
17
 
 
18
  lossy_output_path = os.path.join(tmpdirname, "lossy.jpg")
19
  img.save(lossy_output_path, format="JPEG", quality=50, optimize=True)
20
  lossy_size = os.path.getsize(lossy_output_path) / 1024
21
  lossy_img = Image.open(lossy_output_path)
22
  lossy_diff = original_size - lossy_size
23
 
 
24
  reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
25
  reduced_img = img.resize((img.width // 2, img.height // 2), Image.LANCZOS)
26
  reduced_img.save(reduced_output_path, format="JPEG", quality=85, optimize=True)
@@ -28,19 +31,25 @@ def optimize_image(image):
28
  reduced_img = Image.open(reduced_output_path)
29
  reduced_diff = original_size - reduced_size
30
 
 
31
  webp_lossy_output_path = os.path.join(tmpdirname, "lossy.webp")
32
  img.save(webp_lossy_output_path, format="WEBP", quality=50, optimize=True)
33
  webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024
34
  webp_lossy_img = Image.open(webp_lossy_output_path)
35
  webp_lossy_diff = original_size - webp_lossy_size
36
 
 
37
  return [
38
- lossless_img, f"Sin pérdida: {lossless_size:.2f} KB ({-lossless_diff:.2f} KB)", lossless_output_path,
39
- lossy_img, f"Con pérdida: {lossy_size:.2f} KB ({-lossy_diff:.2f} KB)", lossy_output_path,
40
- reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB ({-reduced_diff:.2f} KB)", reduced_output_path,
41
- webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB ({-webp_lossy_diff:.2f} KB)", webp_lossy_output_path
42
  ]
43
 
 
 
 
 
44
  with gr.Blocks() as demo:
45
  gr.Markdown("### Optimización de imágenes para la web")
46
 
@@ -50,24 +59,29 @@ with gr.Blocks() as demo:
50
  with gr.Row():
51
  with gr.Column():
52
  optimized_output1 = gr.Image(label="Optimización sin pérdida")
53
- optimized_size1 = gr.Text(visible=False)
54
  download_button1 = gr.Button("Descargar")
55
-
 
56
  with gr.Column():
57
  optimized_output2 = gr.Image(label="Optimización con pérdida")
58
- optimized_size2 = gr.Text(visible=False)
59
  download_button2 = gr.Button("Descargar")
 
60
 
61
  with gr.Column():
62
  optimized_output3 = gr.Image(label="Reducción de resolución")
63
- optimized_size3 = gr.Text(visible=False)
64
  download_button3 = gr.Button("Descargar")
 
65
 
66
  with gr.Column():
67
  optimized_output4 = gr.Image(label="Optimización WebP con pérdida")
68
- optimized_size4 = gr.Text(visible=False)
69
  download_button4 = gr.Button("Descargar")
 
70
 
 
71
  optimize_button.click(
72
  fn=optimize_image,
73
  inputs=image_input,
@@ -79,9 +93,10 @@ with gr.Blocks() as demo:
79
  ]
80
  )
81
 
82
- download_button1.click(lambda x: x, inputs=optimized_size1, outputs=download_button1)
83
- download_button2.click(lambda x: x, inputs=optimized_size2, outputs=download_button2)
84
- download_button3.click(lambda x: x, inputs=optimized_size3, outputs=download_button3)
85
- download_button4.click(lambda x: x, inputs=optimized_size4, outputs=download_button4)
 
86
 
87
  demo.launch()
 
9
  original_size = os.path.getsize(image.name) / 1024 # tamaño en KB
10
 
11
  with tempfile.TemporaryDirectory() as tmpdirname:
12
+ # 1. Compresión sin pérdida
13
  lossless_output_path = os.path.join(tmpdirname, "lossless.png")
14
  img.save(lossless_output_path, format="PNG", optimize=True)
15
  lossless_size = os.path.getsize(lossless_output_path) / 1024
16
  lossless_img = Image.open(lossless_output_path)
17
  lossless_diff = original_size - lossless_size
18
 
19
+ # 2. Compresión con pérdida (JPEG)
20
  lossy_output_path = os.path.join(tmpdirname, "lossy.jpg")
21
  img.save(lossy_output_path, format="JPEG", quality=50, optimize=True)
22
  lossy_size = os.path.getsize(lossy_output_path) / 1024
23
  lossy_img = Image.open(lossy_output_path)
24
  lossy_diff = original_size - lossy_size
25
 
26
+ # 3. Reducción de resolución
27
  reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
28
  reduced_img = img.resize((img.width // 2, img.height // 2), Image.LANCZOS)
29
  reduced_img.save(reduced_output_path, format="JPEG", quality=85, optimize=True)
 
31
  reduced_img = Image.open(reduced_output_path)
32
  reduced_diff = original_size - reduced_size
33
 
34
+ # 4. Compresión con WebP (con pérdida)
35
  webp_lossy_output_path = os.path.join(tmpdirname, "lossy.webp")
36
  img.save(webp_lossy_output_path, format="WEBP", quality=50, optimize=True)
37
  webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024
38
  webp_lossy_img = Image.open(webp_lossy_output_path)
39
  webp_lossy_diff = original_size - webp_lossy_size
40
 
41
+ # Retornar las imágenes optimizadas, tamaños y rutas para descarga
42
  return [
43
+ lossless_img, f"Sin pérdida: {lossless_size:.2f} KB (diferencia: {-lossless_diff:.2f} KB)", lossless_output_path,
44
+ lossy_img, f"Con pérdida: {lossy_size:.2f} KB (diferencia: {-lossy_diff:.2f} KB)", lossy_output_path,
45
+ reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB (diferencia: {-reduced_diff:.2f} KB)", reduced_output_path,
46
+ webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB (diferencia: {-webp_lossy_diff:.2f} KB)", webp_lossy_output_path
47
  ]
48
 
49
+ def download_image(file_path):
50
+ # Función para servir el archivo para descarga
51
+ return gr.File.update(value=file_path, visible=True)
52
+
53
  with gr.Blocks() as demo:
54
  gr.Markdown("### Optimización de imágenes para la web")
55
 
 
59
  with gr.Row():
60
  with gr.Column():
61
  optimized_output1 = gr.Image(label="Optimización sin pérdida")
62
+ optimized_size1 = gr.Text()
63
  download_button1 = gr.Button("Descargar")
64
+ download_file1 = gr.File()
65
+
66
  with gr.Column():
67
  optimized_output2 = gr.Image(label="Optimización con pérdida")
68
+ optimized_size2 = gr.Text()
69
  download_button2 = gr.Button("Descargar")
70
+ download_file2 = gr.File()
71
 
72
  with gr.Column():
73
  optimized_output3 = gr.Image(label="Reducción de resolución")
74
+ optimized_size3 = gr.Text()
75
  download_button3 = gr.Button("Descargar")
76
+ download_file3 = gr.File()
77
 
78
  with gr.Column():
79
  optimized_output4 = gr.Image(label="Optimización WebP con pérdida")
80
+ optimized_size4 = gr.Text()
81
  download_button4 = gr.Button("Descargar")
82
+ download_file4 = gr.File()
83
 
84
+ # Conectar la optimización con los outputs
85
  optimize_button.click(
86
  fn=optimize_image,
87
  inputs=image_input,
 
93
  ]
94
  )
95
 
96
+ # Conectar los botones de descarga
97
+ download_button1.click(download_image, inputs=download_button1, outputs=download_file1)
98
+ download_button2.click(download_image, inputs=download_button2, outputs=download_file2)
99
+ download_button3.click(download_image, inputs=download_button3, outputs=download_file3)
100
+ download_button4.click(download_image, inputs=download_button4, outputs=download_file4)
101
 
102
  demo.launch()