webp_to_png_gui/webp_to_png/tools.py
2024-05-05 17:50:32 +08:00

27 lines
957 B
Python

import os
from typing import Any
from PIL import Image
def convert_webp_to_png(webp_dir:str, png_dir:str,window:Any)->None:
# 获取目录中以.webp结尾的文件列表
webp_files = [f for f in os.listdir(webp_dir) if f.endswith('.webp')]
# 获取文件总数
nums = len(webp_files)
# 初始化进度条计数器
times = 0
# 遍历所有webp文件
for file_name in webp_files:
# 构建webp文件的路径
webp_file_path = os.path.join(webp_dir, file_name)
# 构建输出的png文件路径
png_file_path = os.path.join(png_dir, os.path.splitext(file_name)[0] + '.png')
# 打开webp文件并保存为png
with Image.open(webp_file_path) as img:
img.save(png_file_path, 'PNG')
# 更新进度条
times += 1
progress = int(times/nums*100)
window["bar"].update(current_count=progress)
print("Conversion completed.")