Merlintxu commited on
Commit
853d6f2
1 Parent(s): fc6a1b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -38
app.py CHANGED
@@ -8,44 +8,49 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
8
 
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 (PNG)
13
- lossless_output_path = os.path.join(tmpdirname, "lossless.png")
14
- img.save(lossless_output_path, format="PNG", optimize=png_optimize)
15
- lossless_size = os.path.getsize(lossless_output_path) / 1024
16
- lossless_diff = original_size - lossless_size
17
- lossless_img = Image.open(lossless_output_path)
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=jpeg_quality, optimize=True)
22
- lossy_size = os.path.getsize(lossy_output_path) / 1024
23
- lossy_diff = original_size - lossy_size
24
- lossy_img = Image.open(lossy_output_path)
25
-
26
- # 3. Reducción de resolución (JPEG)
27
- reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
28
- new_resolution = (img.width * jpeg_resolution // 100, img.height * jpeg_resolution // 100)
29
- reduced_img = img.resize(new_resolution, Image.LANCZOS)
30
- reduced_img.save(reduced_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
31
- reduced_size = os.path.getsize(reduced_output_path) / 1024
32
- reduced_diff = original_size - reduced_size
33
- reduced_img = Image.open(reduced_output_path)
34
-
35
- # 4. Compresión con WebP (con pérdida)
36
- webp_lossy_output_path = os.path.join(tmpdirname, "lossy.webp")
37
- img.save(webp_lossy_output_path, format="WEBP", quality=webp_quality, optimize=True)
38
- webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024
39
- webp_lossy_diff = original_size - webp_lossy_size
40
- webp_lossy_img = Image.open(webp_lossy_output_path)
41
-
42
- # Retornar las imágenes optimizadas, tamaños y rutas para descarga
43
- return [
44
- lossless_img, f"Sin pérdida: {lossless_size:.2f} KB ({-lossless_diff:.2f} KB)", lossless_output_path,
45
- lossy_img, f"Con pérdida: {lossy_size:.2f} KB ({-lossy_diff:.2f} KB)", lossy_output_path,
46
- reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB ({-reduced_diff:.2f} KB)", reduced_output_path,
47
- webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB ({-webp_lossy_diff:.2f} KB)", webp_lossy_output_path
48
- ]
 
 
 
 
 
49
 
50
  def download_image(file_path):
51
  return gr.File.update(value=file_path, label="Descargar", visible=True)
@@ -85,6 +90,7 @@ with gr.Blocks() as demo:
85
  download_button4 = gr.Button("Descargar")
86
  download_file4 = gr.File(visible=False)
87
 
 
88
  optimize_button.click(
89
  fn=optimize_image,
90
  inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
@@ -96,6 +102,50 @@ with gr.Blocks() as demo:
96
  ]
97
  )
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  download_button1.click(download_image, inputs=download_file1, outputs=download_file1)
100
  download_button2.click(download_image, inputs=download_file2, outputs=download_file2)
101
  download_button3.click(download_image, inputs=download_file3, outputs=download_file3)
 
8
 
9
  original_size = os.path.getsize(image.name) / 1024 # tamaño en KB
10
 
11
+ # Crear un directorio seguro para almacenar las imágenes optimizadas
12
+ output_dir = "/tmp/optimized_images"
13
+ os.makedirs(output_dir, exist_ok=True)
14
+
15
+ # Generar archivos temporales en un directorio seguro
16
+ lossless_output_path = os.path.join(output_dir, "lossless.png")
17
+ lossy_output_path = os.path.join(output_dir, "lossy.jpg")
18
+ reduced_output_path = os.path.join(output_dir, "reduced_resolution.jpg")
19
+ webp_lossy_output_path = os.path.join(output_dir, "lossy.webp")
20
+
21
+ # 1. Compresión sin pérdida (PNG)
22
+ img.save(lossless_output_path, format="PNG", optimize=png_optimize)
23
+ lossless_size = os.path.getsize(lossless_output_path) / 1024
24
+ lossless_diff = original_size - lossless_size
25
+ lossless_img = Image.open(lossless_output_path)
26
+
27
+ # 2. Compresión con pérdida (JPEG)
28
+ img.save(lossy_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
29
+ lossy_size = os.path.getsize(lossy_output_path) / 1024
30
+ lossy_diff = original_size - lossy_size
31
+ lossy_img = Image.open(lossy_output_path)
32
+
33
+ # 3. Reducción de resolución (JPEG)
34
+ new_resolution = (img.width * jpeg_resolution // 100, img.height * jpeg_resolution // 100)
35
+ reduced_img = img.resize(new_resolution, Image.LANCZOS)
36
+ reduced_img.save(reduced_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
37
+ reduced_size = os.path.getsize(reduced_output_path) / 1024
38
+ reduced_diff = original_size - reduced_size
39
+ reduced_img = Image.open(reduced_output_path)
40
+
41
+ # 4. Compresión con WebP (con pérdida)
42
+ img.save(webp_lossy_output_path, format="WEBP", quality=webp_quality, optimize=True)
43
+ webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024
44
+ webp_lossy_diff = original_size - webp_lossy_size
45
+ webp_lossy_img = Image.open(webp_lossy_output_path)
46
+
47
+ # Retornar las imágenes optimizadas, tamaños y rutas para descarga
48
+ return [
49
+ lossless_img, f"Sin pérdida: {lossless_size:.2f} KB (diferencia: {-lossless_diff:.2f} KB)", lossless_output_path,
50
+ lossy_img, f"Con pérdida: {lossy_size:.2f} KB (diferencia: {-lossy_diff:.2f} KB)", lossy_output_path,
51
+ reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB (diferencia: {-reduced_diff:.2f} KB)", reduced_output_path,
52
+ webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB (diferencia: {-webp_lossy_diff:.2f} KB)", webp_lossy_output_path
53
+ ]
54
 
55
  def download_image(file_path):
56
  return gr.File.update(value=file_path, label="Descargar", visible=True)
 
90
  download_button4 = gr.Button("Descargar")
91
  download_file4 = gr.File(visible=False)
92
 
93
+ # Conectar cada control a la función de optimización
94
  optimize_button.click(
95
  fn=optimize_image,
96
  inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
 
102
  ]
103
  )
104
 
105
+ png_optimize.change(
106
+ fn=optimize_image,
107
+ inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
108
+ outputs=[
109
+ optimized_output1, optimized_size1, download_file1,
110
+ optimized_output2, optimized_size2, download_file2,
111
+ optimized_output3, optimized_size3, download_file3,
112
+ optimized_output4, optimized_size4, download_file4
113
+ ]
114
+ )
115
+
116
+ jpeg_quality.change(
117
+ fn=optimize_image,
118
+ inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
119
+ outputs=[
120
+ optimized_output1, optimized_size1, download_file1,
121
+ optimized_output2, optimized_size2, download_file2,
122
+ optimized_output3, optimized_size3, download_file3,
123
+ optimized_output4, optimized_size4, download_file4
124
+ ]
125
+ )
126
+
127
+ jpeg_resolution.change(
128
+ fn=optimize_image,
129
+ inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
130
+ outputs=[
131
+ optimized_output1, optimized_size1, download_file1,
132
+ optimized_output2, optimized_size2, download_file2,
133
+ optimized_output3, optimized_size3, download_file3,
134
+ optimized_output4, optimized_size4, download_file4
135
+ ]
136
+ )
137
+
138
+ webp_quality.change(
139
+ fn=optimize_image,
140
+ inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
141
+ outputs=[
142
+ optimized_output1, optimized_size1, download_file1,
143
+ optimized_output2, optimized_size2, download_file2,
144
+ optimized_output3, optimized_size3, download_file3,
145
+ optimized_output4, optimized_size4, download_file4
146
+ ]
147
+ )
148
+
149
  download_button1.click(download_image, inputs=download_file1, outputs=download_file1)
150
  download_button2.click(download_image, inputs=download_file2, outputs=download_file2)
151
  download_button3.click(download_image, inputs=download_file3, outputs=download_file3)