python语言gif动画制作程序代码QZQ

from PIL import Image, ImageTk
import tkinter as tk
from tkinter import filedialog
from tkinter import Scale

class GIFPlayer:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("GIF Player")

        # 图像框

        self.image_frame = tk.Frame(self.root)
        self.image_frame.place(x=150, y=80, width=800, height=600)




        self.image_label = tk.Label(self.image_frame)
        self.image_label.pack()

        # 播放按钮
        self.play_button = tk.Button(self.root, text="Play", command=self.play_gif)
        self.play_button.place(x=100, y=0, width=100, height=50)

        # 保存按钮
        self.save_button = tk.Button(self.root, text="Save as GIF", command=self.save_gif)
        self.save_button.place(x=200, y=0, width=100, height=50)

        # 打开图像按钮
        self.open_button = tk.Button(self.root, text="Open Images", command=self.open_images)
        self.open_button.place(x=0, y=0, width=100, height=50)

        # 列表框
        self.listbox = tk.Listbox(self.root, selectmode=tk.MULTIPLE)
        self.listbox.place(x=0, y=100, width=100, height=200)

        # 播放速度滑块
        self.speed_scale = Scale(self.root, from_=10, to=500, orient=tk.HORIZONTAL, label="播放速度(毫秒)")
        self.speed_scale.set(100)
        self.speed_scale.place(x=300, y=0, width=200, height=50)

        self.image_paths = []
        self.images = []

        self.current_frame_index = 0
        self.delay = self.speed_scale.get()

    def open_images(self):
        file_paths = filedialog.askopenfilenames(filetypes=[("Image files", "*.png;*.jpg;*.jpeg")])
        for path in file_paths:
            self.image_paths.append(path)
            im = Image.open(path)
            self.images.append(im)
            self.listbox.insert(tk.END, path)

    def play_gif(self):
        if self.images:
            img = ImageTk.PhotoImage(self.images[self.current_frame_index])
            self.image_label.config(image=img)
            self.image_label.image = img
            self.current_frame_index = (self.current_frame_index + 1) % len(self.images)
            self.delay = self.speed_scale.get()
            self.root.after(self.delay, self.play_gif)

    def save_gif(self):
        selected_indices = self.listbox.curselection()
        if selected_indices:
            selected_images = [self.images[i] for i in selected_indices]
            if selected_images:
                file_path = filedialog.asksaveasfilename(defaultextension=".gif", filetypes=[("GIF files", "*.gif")])
                if file_path:
                    # 创建一个新的图像对象列表,每个图像重复多次以实现无限循环效果
                    repeated_images = []
                    for img in selected_images:
                        for _ in range(1):  # 增加重复次数以增强无限循环的感觉
                            repeated_images.append(img.copy())
                    first_image = repeated_images[0]
                    first_image.save(file_path, save_all=True, append_images=repeated_images[1:], optimize=False, duration=self.delay, loop=0)  # 设置 loop=0 实现无限循环

if __name__ == "__main__":
    player = GIFPlayer()
    player.root.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YHN282615109

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值