From 8697b83dc0d439743da04da6a131a978c5b6f154 Mon Sep 17 00:00:00 2001 From: ZtRXR Date: Sat, 4 May 2024 12:12:57 +0800 Subject: [PATCH] reconstruct --- webp_to_png/__init__.py | 0 webp_to_png/__main__.pyw | 44 +++++++--------------------------------- webp_to_png/tools.py | 26 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 37 deletions(-) delete mode 100644 webp_to_png/__init__.py create mode 100644 webp_to_png/tools.py diff --git a/webp_to_png/__init__.py b/webp_to_png/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/webp_to_png/__main__.pyw b/webp_to_png/__main__.pyw index ede56e6..ebe92ed 100644 --- a/webp_to_png/__main__.pyw +++ b/webp_to_png/__main__.pyw @@ -1,12 +1,6 @@ -import asyncio import threading -from time import sleep -from typing import Any -from PIL import Image -import os import PySimpleGUI as sg - -import webp_to_png +from tools import convert_webp_to_png layout = [ [sg.Text("请选择webp文件夹:"),sg.Text("未选择文件夹"),sg.FolderBrowse("点击选择文件夹",key="input_folder")], @@ -18,33 +12,6 @@ layout = [ window = sg.Window("批量webp格式转化为png格式 -by Zengtudor",layout,size=(800,400),resizable=True) bar = window["bar"] -def convert_webp_to_png(webp_dir, png_dir): - # 获取目录中以.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.write_event_value("bar",{"current_count":progress} ) - print("Conversion completed.") - - - def main(): while True: ret = window.read() @@ -54,16 +21,19 @@ def main(): if event == sg.WIN_CLOSED : break elif event == "ok": # window.write_event_value("bar", {"current_count":50}) - print(value["input_folder"] and value["output_folder"]) + print(value["input_folder"] and value["output_folder"],"bool") if value["input_folder"] and value["output_folder"] : print("start") # convert_webp_to_png(value["input_folder"],value["output_folder"]) - threading.Thread(target=convert_webp_to_png,args=[value["input_folder"],value["output_folder"]],daemon=True).start() + threading.Thread(target=convert_webp_to_png, + args=[value["input_folder"], value["output_folder"], window] + ,daemon=True + ).start() else : sg.popup("你还没有选择路径!") elif event == "bar": print("freshing bar") bar.update(current_count=value["bar"]["current_count"]) - window.refresh() + # window.refresh() if __name__ == "__main__": diff --git a/webp_to_png/tools.py b/webp_to_png/tools.py new file mode 100644 index 0000000..5f8b19c --- /dev/null +++ b/webp_to_png/tools.py @@ -0,0 +1,26 @@ +import os +from PIL import Image +def convert_webp_to_png(webp_dir:str, png_dir:str,window)->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.write_event_value("bar",{"current_count":progress} ) + print("Conversion completed.") \ No newline at end of file