webp_to_png_gui/webp_to_png/tools.py

27 lines
957 B
Python
Raw Permalink Normal View History

2024-05-04 04:12:57 +00:00
import os
2024-05-04 04:14:56 +00:00
from typing import Any
2024-05-04 04:12:57 +00:00
from PIL import Image
2024-05-04 04:14:56 +00:00
def convert_webp_to_png(webp_dir:str, png_dir:str,window:Any)->None:
2024-05-04 04:12:57 +00:00
# 获取目录中以.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)
2024-05-05 09:50:32 +00:00
window["bar"].update(current_count=progress)
2024-05-04 04:12:57 +00:00
print("Conversion completed.")