Skip to content

Commit 1f18688

Browse files
authored
Python script convert png to webp
1 parent cc0d74c commit 1f18688

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

aWebp.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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"\nConversión completada: {converted} de {total_files} archivos convertidos a WebP")
34+
35+
# Ejemplo de uso
36+
convert_png_to_webp('./avatars', './avatars_webp')

0 commit comments

Comments
 (0)