Merlintxu commited on
Commit
95fc1cb
verified
1 Parent(s): ad49d4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -5
app.py CHANGED
@@ -3,8 +3,19 @@ from PIL import Image
3
  import os
4
  import tempfile
5
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def convert_image_format(image, target_format):
7
- # Crear un directorio temporal para guardar la imagen
8
  with tempfile.TemporaryDirectory() as tmpdirname:
9
  # Crear una ruta para el archivo de salida en el directorio temporal
10
  output_name = os.path.join(tmpdirname, f"output_image.{target_format.lower()}")
@@ -15,8 +26,11 @@ def convert_image_format(image, target_format):
15
  # Guardar la imagen en el formato deseado
16
  img.save(output_name, format=target_format.upper())
17
 
18
- # Devolver la ruta del archivo convertido
19
- return output_name
 
 
 
20
 
21
  # Interfaz de Gradio
22
  with gr.Blocks() as demo:
@@ -26,8 +40,15 @@ with gr.Blocks() as demo:
26
  format_dropdown = gr.Dropdown(label="Selecciona el formato de salida", choices=["jpg", "png", "webp"], value="webp")
27
  convert_button = gr.Button("Convertir")
28
 
29
- output_gallery = gr.Gallery(label="Imagen convertida")
 
 
 
 
 
 
30
 
31
- convert_button.click(fn=convert_image_format, inputs=[image_input, format_dropdown], outputs=output_gallery)
 
32
 
33
  demo.launch()
 
3
  import os
4
  import tempfile
5
 
6
+ def get_image_info(image):
7
+ # Obtener la ruta del archivo
8
+ image_path = image.name
9
+
10
+ # Obtener el tama帽o del archivo en KB
11
+ file_size = os.path.getsize(image_path) / 1024 # Convertir a KB
12
+
13
+ # Abrir la imagen para mostrarla
14
+ img = Image.open(image_path)
15
+
16
+ return img, f"Tama帽o del archivo: {file_size:.2f} KB"
17
+
18
  def convert_image_format(image, target_format):
 
19
  with tempfile.TemporaryDirectory() as tmpdirname:
20
  # Crear una ruta para el archivo de salida en el directorio temporal
21
  output_name = os.path.join(tmpdirname, f"output_image.{target_format.lower()}")
 
26
  # Guardar la imagen en el formato deseado
27
  img.save(output_name, format=target_format.upper())
28
 
29
+ # Calcular el tama帽o del nuevo archivo
30
+ file_size = os.path.getsize(output_name) / 1024 # Convertir a KB
31
+
32
+ # Devolver la imagen convertida y su tama帽o
33
+ return output_name, f"Tama帽o del archivo convertido: {file_size:.2f} KB"
34
 
35
  # Interfaz de Gradio
36
  with gr.Blocks() as demo:
 
40
  format_dropdown = gr.Dropdown(label="Selecciona el formato de salida", choices=["jpg", "png", "webp"], value="webp")
41
  convert_button = gr.Button("Convertir")
42
 
43
+ # Mostrar la imagen original y su tama帽o
44
+ image_output = gr.Image(label="Imagen Original")
45
+ size_output = gr.Text(label="Informaci贸n del Archivo")
46
+
47
+ # Mostrar la imagen convertida y su tama帽o
48
+ converted_image_output = gr.Image(label="Imagen Convertida")
49
+ converted_size_output = gr.Text(label="Informaci贸n del Archivo Convertido")
50
 
51
+ image_input.change(fn=get_image_info, inputs=image_input, outputs=[image_output, size_output])
52
+ convert_button.click(fn=convert_image_format, inputs=[image_input, format_dropdown], outputs=[converted_image_output, converted_size_output])
53
 
54
  demo.launch()