File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ from PIL import Image
2+ import os
3+
4+ def convert_png_to_webp (input_folder , output_folder ):
5+ # Crear carpeta de salida si no existe
6+ if not os .path .exists (output_folder ):
7+ os .makedirs (output_folder )
8+
9+ # Obtener lista de archivos PNG
10+ png_files = [f for f in os .listdir (input_folder ) if f .lower ().endswith ('.png' )]
11+ total_files = len (png_files )
12+
13+ print (f"Encontrados { total_files } archivos PNG para convertir" )
14+
15+ # Contador de archivos procesados
16+ converted = 0
17+
18+ # Procesar cada archivo
19+ for filename in png_files :
20+ input_path = os .path .join (input_folder , filename )
21+ output_filename = os .path .splitext (filename )[0 ] + '.webp'
22+ output_path = os .path .join (output_folder , output_filename )
23+
24+ # Abrir y convertir la imagen
25+ img = Image .open (input_path )
26+ img .save (output_path , 'WEBP' , lossless = True , quality = 90 )
27+
28+ # Actualizar contador y mostrar progreso
29+ converted += 1
30+ progress = (converted / total_files ) * 100
31+ print (f'[{ converted } /{ total_files } ] ({ progress :.1f} %) Convertido: { filename } -> { output_filename } ' )
32+
33+ print (f"\n Conversión completada: { converted } de { total_files } archivos convertidos a WebP" )
34+
35+ # Ejemplo de uso
36+ convert_png_to_webp ('./avatars' , './avatars_webp' )
You can’t perform that action at this time.
0 commit comments