Merlintxu commited on
Commit
1fa88c0
1 Parent(s): 1ce0886

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -21
app.py CHANGED
@@ -13,14 +13,12 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
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_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=jpeg_quality, 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 (JPEG)
@@ -29,14 +27,12 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
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_img = Image.open(reduced_output_path)
33
  reduced_diff = original_size - reduced_size
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_img = Image.open(webp_lossy_output_path)
40
  webp_lossy_diff = original_size - webp_lossy_size
41
 
42
  # Retornar las imágenes optimizadas, tamaños y rutas para descarga
@@ -48,7 +44,7 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
48
  ]
49
 
50
  def download_image(file_path):
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")
@@ -60,45 +56,45 @@ with gr.Blocks() as demo:
60
  with gr.Column():
61
  optimized_output1 = gr.Image(label="Optimización sin pérdida")
62
  png_optimize = gr.Checkbox(label="Optimizar PNG", value=True)
63
- optimized_size1 = gr.Text()
64
  download_button1 = gr.Button("Descargar")
65
- download_file1 = gr.File()
66
 
67
  with gr.Column():
68
  optimized_output2 = gr.Image(label="Optimización con pérdida (JPEG)")
69
  jpeg_quality = gr.Slider(label="Calidad JPEG", minimum=10, maximum=100, value=50, step=1)
70
- optimized_size2 = gr.Text()
71
  download_button2 = gr.Button("Descargar")
72
- download_file2 = gr.File()
73
 
74
  with gr.Column():
75
  optimized_output3 = gr.Image(label="Reducción de resolución (JPEG)")
76
  jpeg_resolution = gr.Slider(label="Resolución JPEG (%)", minimum=10, maximum=100, value=50, step=1)
77
- optimized_size3 = gr.Text()
78
  download_button3 = gr.Button("Descargar")
79
- download_file3 = gr.File()
80
 
81
  with gr.Column():
82
  optimized_output4 = gr.Image(label="Optimización WebP con pérdida")
83
  webp_quality = gr.Slider(label="Calidad WebP", minimum=10, maximum=100, value=50, step=1)
84
- optimized_size4 = gr.Text()
85
  download_button4 = gr.Button("Descargar")
86
- download_file4 = gr.File()
87
 
88
  optimize_button.click(
89
  fn=optimize_image,
90
  inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
91
  outputs=[
92
- optimized_output1, optimized_size1, download_button1,
93
- optimized_output2, optimized_size2, download_button2,
94
- optimized_output3, optimized_size3, download_button3,
95
- optimized_output4, optimized_size4, download_button4
96
  ]
97
  )
98
 
99
- download_button1.click(download_image, inputs=download_button1, outputs=download_file1)
100
- download_button2.click(download_image, inputs=download_button2, outputs=download_file2)
101
- download_button3.click(download_image, inputs=download_button3, outputs=download_file3)
102
- download_button4.click(download_image, inputs=download_button4, outputs=download_file4)
103
 
104
  demo.launch()
 
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
 
18
  # 2. Compresión con pérdida (JPEG)
19
  lossy_output_path = os.path.join(tmpdirname, "lossy.jpg")
20
  img.save(lossy_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
21
  lossy_size = os.path.getsize(lossy_output_path) / 1024
 
22
  lossy_diff = original_size - lossy_size
23
 
24
  # 3. Reducción de resolución (JPEG)
 
27
  reduced_img = img.resize(new_resolution, Image.LANCZOS)
28
  reduced_img.save(reduced_output_path, format="JPEG", quality=jpeg_quality, optimize=True)
29
  reduced_size = os.path.getsize(reduced_output_path) / 1024
 
30
  reduced_diff = original_size - reduced_size
31
 
32
  # 4. Compresión con WebP (con pérdida)
33
  webp_lossy_output_path = os.path.join(tmpdirname, "lossy.webp")
34
  img.save(webp_lossy_output_path, format="WEBP", quality=webp_quality, optimize=True)
35
  webp_lossy_size = os.path.getsize(webp_lossy_output_path) / 1024
 
36
  webp_lossy_diff = original_size - webp_lossy_size
37
 
38
  # Retornar las imágenes optimizadas, tamaños y rutas para descarga
 
44
  ]
45
 
46
  def download_image(file_path):
47
+ return gr.File.update(value=file_path, label="Descargar", visible=True)
48
 
49
  with gr.Blocks() as demo:
50
  gr.Markdown("### Optimización de imágenes para la web")
 
56
  with gr.Column():
57
  optimized_output1 = gr.Image(label="Optimización sin pérdida")
58
  png_optimize = gr.Checkbox(label="Optimizar PNG", value=True)
59
+ optimized_size1 = gr.Text(value="", interactive=False, show_label=False)
60
  download_button1 = gr.Button("Descargar")
61
+ download_file1 = gr.File(visible=False)
62
 
63
  with gr.Column():
64
  optimized_output2 = gr.Image(label="Optimización con pérdida (JPEG)")
65
  jpeg_quality = gr.Slider(label="Calidad JPEG", minimum=10, maximum=100, value=50, step=1)
66
+ optimized_size2 = gr.Text(value="", interactive=False, show_label=False)
67
  download_button2 = gr.Button("Descargar")
68
+ download_file2 = gr.File(visible=False)
69
 
70
  with gr.Column():
71
  optimized_output3 = gr.Image(label="Reducción de resolución (JPEG)")
72
  jpeg_resolution = gr.Slider(label="Resolución JPEG (%)", minimum=10, maximum=100, value=50, step=1)
73
+ optimized_size3 = gr.Text(value="", interactive=False, show_label=False)
74
  download_button3 = gr.Button("Descargar")
75
+ download_file3 = gr.File(visible=False)
76
 
77
  with gr.Column():
78
  optimized_output4 = gr.Image(label="Optimización WebP con pérdida")
79
  webp_quality = gr.Slider(label="Calidad WebP", minimum=10, maximum=100, value=50, step=1)
80
+ optimized_size4 = gr.Text(value="", interactive=False, show_label=False)
81
  download_button4 = gr.Button("Descargar")
82
+ download_file4 = gr.File(visible=False)
83
 
84
  optimize_button.click(
85
  fn=optimize_image,
86
  inputs=[image_input, png_optimize, jpeg_quality, jpeg_resolution, webp_quality],
87
  outputs=[
88
+ optimized_output1, optimized_size1, download_file1,
89
+ optimized_output2, optimized_size2, download_file2,
90
+ optimized_output3, optimized_size3, download_file3,
91
+ optimized_output4, optimized_size4, download_file4
92
  ]
93
  )
94
 
95
+ download_button1.click(download_image, inputs=download_file1, outputs=download_file1)
96
+ download_button2.click(download_image, inputs=download_file2, outputs=download_file2)
97
+ download_button3.click(download_image, inputs=download_file3, outputs=download_file3)
98
+ download_button4.click(download_image, inputs=download_file4, outputs=download_file4)
99
 
100
  demo.launch()