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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -14,12 +14,14 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
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)
25
  reduced_output_path = os.path.join(tmpdirname, "reduced_resolution.jpg")
@@ -28,19 +30,21 @@ def optimize_image(image, png_optimize, jpeg_quality, jpeg_resolution, webp_qual
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
39
  return [
40
- lossless_img, f"Sin pérdida: {lossless_size:.2f} KB (diferencia: {-lossless_diff:.2f} KB)", lossless_output_path,
41
- lossy_img, f"Con pérdida: {lossy_size:.2f} KB (diferencia: {-lossy_diff:.2f} KB)", lossy_output_path,
42
- reduced_img, f"Reducción de resolución: {reduced_size:.2f} KB (diferencia: {-reduced_diff:.2f} KB)", reduced_output_path,
43
- webp_lossy_img, f"WebP con pérdida: {webp_lossy_size:.2f} KB (diferencia: {-webp_lossy_diff:.2f} KB)", webp_lossy_output_path
44
  ]
45
 
46
  def download_image(file_path):
 
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")
 
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):