import os
from PIL import Image
import tkinter as tk
from tkinter import filedialog, messagebox
 
def convert_webp_to_png(directory_path):
    converted_count = 0
    for root, dirs, files in os.walk(directory_path):
        for file in files:
            if file.endswith('.webp'):
                webp_path = os.path.join(root, file)
                png_path = os.path.splitext(webp_path)[0] + '.png'
                try:
                    image = Image.open(webp_path)
                    image.save(png_path, 'PNG')
                    os.remove(webp_path)
                    converted_count += 1
                except Exception as e:
                    messagebox.showerror("转换错误", f"文件'{file}'转换失败:{str(e)}")
 
    messagebox.showinfo("转换完成", f"成功转换 {converted_count} 个文件!")
 
def select_directory():
    directory = filedialog.askdirectory()
    if directory:
        directory_label.config(text=directory)
 
def convert_files():
    directory_path = directory_label.cget("text")
    if directory_path:
        convert_webp_to_png(directory_path)
 
# 创建主窗口
window = tk.Tk()
window.title("WebP转换PNG- - - -云时出品- -")
window.geometry("350x100")
 
# 创建选择目录按钮
select_button = tk.Button(window, text="选择目录", command=select_directory)
select_button.pack()
 
# 创建显示目录路径的标签
directory_label = tk.Label(window, text="")
directory_label.pack()
 
# 创建执行转换按钮
convert_button = tk.Button(window, text="执行转换", command=convert_files)
convert_button.pack()
 
# 显示窗口
window.mainloop()

上面是源代码   下面是生成后的文件

1.jpg

 

该资源来源于WordPress主站,点击下方按钮前往原文页面获取完整内容:

跳转至 WordPress 主站原文页面

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。