46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
import tkinter as tk
|
||
from tkinter import filedialog, messagebox
|
||
from PIL import Image
|
||
|
||
class GIFMakerApp:
|
||
def __init__(self, root):
|
||
self.root = root
|
||
self.root.title("GIF Maker")
|
||
|
||
# 选择图片按钮
|
||
self.select_button = tk.Button(root, text="选择图片", command=self.select_images)
|
||
self.select_button.pack(pady=10)
|
||
|
||
# 保存 GIF 按钮
|
||
self.save_button = tk.Button(root, text="保存 GIF", command=self.save_gif, state=tk.DISABLED)
|
||
self.save_button.pack(pady=10)
|
||
|
||
self.images = [] # 保存选择的图片
|
||
|
||
def select_images(self):
|
||
# 选择多张图片
|
||
image_paths = filedialog.askopenfilenames(title="选择图片", filetypes=[("Image files", "*.jpg;*.png;*.jpeg;*.bmp"), ("All files", "*.*")])
|
||
if image_paths:
|
||
self.images = [Image.open(image_path) for image_path in image_paths] # 打开所有选择的图片
|
||
messagebox.showinfo("已选择图片", f"已选择 {len(self.images)} 张图片")
|
||
self.save_button.config(state=tk.NORMAL) # 启用保存按钮
|
||
|
||
def save_gif(self):
|
||
if not self.images:
|
||
messagebox.showwarning("警告", "没有选择图片!")
|
||
return
|
||
|
||
# 设置保存文件名
|
||
save_path = filedialog.asksaveasfilename(defaultextension=".gif", filetypes=[("GIF files", "*.gif")])
|
||
if save_path:
|
||
# 保存为GIF,指定每帧的时间间隔
|
||
self.images[0].save(save_path, save_all=True, append_images=self.images[1:], duration=10, loop=0)
|
||
messagebox.showinfo("成功", f"GIF 已保存到: {save_path}")
|
||
|
||
# 启动 GUI 应用
|
||
if __name__ == "__main__":
|
||
root = tk.Tk()
|
||
app = GIFMakerApp(root)
|
||
root.geometry("300x200")
|
||
root.mainloop()
|