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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -3,37 +3,38 @@ from PIL import Image
3
  import os
4
  import tempfile
5
 
6
- def optimize_image(image):
7
  img = Image.open(image)
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
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)
 
30
  reduced_size = os.path.getsize(reduced_output_path) / 1024
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
@@ -47,7 +48,6 @@ def optimize_image(image):
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:
@@ -59,32 +59,35 @@ with gr.Blocks() as demo:
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,
88
  outputs=[
89
  optimized_output1, optimized_size1, download_button1,
90
  optimized_output2, optimized_size2, download_button2,
@@ -93,7 +96,6 @@ with gr.Blocks() as demo:
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)
 
3
  import os
4
  import tempfile
5
 
6
+ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_quality):
7
  img = Image.open(image)
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_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)
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_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
 
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:
 
59
  with gr.Row():
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,
 
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)