自己是做电商的,有时候保存下来的图片是webp格式的.
写了个小工具.能把目录内的webp格式的图片转化成png.
https://crk.lanzouu.com/iZdWG2mla0wd
Python代码一起附上.
[Python] 纯文本查看 复制代码
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()