python tkinter 使用 PIL 显示动画 GIF

发布于 2024-12-13 09:03:34 字数 722 浏览 7 评论 0原文

有没有办法使用 Python 图像库在 Tkinter 中显示动画 GIF?

我认为 ImageSequence 模块 是这样做的方法,但是我不知道如何使用它以及是否可能。

第一个问题是有没有简单的方法。例如:使用 PIL 和 ImageSequence 加载 GIF,然后使用 ImageTk.PhotoImage 将其绘制在 Tkinter 窗口上,它将被动画化。

或者我是否必须自己设置一个函数,使用 after 方法或类似 time.sleep 的方法来循环 GIF 帧并将它们绘制在 tkinter 窗口上?

第二个问题:即使我必须创建一个函数来循环 GIF 帧,ImageSequence 模块是否应该执行此操作,或者 PIL 有另一个模块可以执行此操作?

我正在使用 Python 3.1 和 PIL 的私有端口,指出在此主题中。

Is there any way to display an animated GIF in Tkinter using Python Image Library?

I thought the ImageSequence module would be the way to do it, but I don't know how to use it and if it's possible.

The first question is if there is any easy way. For example: load a GIF using PIL and the ImageSequence and just draw it on a Tkinter window using ImageTk.PhotoImage and it will be animated.

Or do I have to set up a function myself, using the after method or something like time.sleep to loop through the GIF frames and draw them on a tkinter window?

The second question: even if I have to make a function to loop through the GIF frames, is the ImageSequence module supposed to do this or PIL has another module for it?

I'm using Python 3.1 and a private port of PIL, indicated in this topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-12-20 09:03:34

新闻组:comp.lang.python

来自:“弗雷德里克·伦德”

日期:2006 年 5 月 1 日星期一

丹尼尔·诺格拉迪写道:

'1.1.4版本的源码发行版附带一个Scripts
您可以在其中找到player.py、gifmaker.py和explode.py的目录
它们都处理动画 gif。'

它们仍然随 1.1.5(和 1.1.6)一起提供,并且应该可以工作。

如果您缺少的只是脚本目录中的一些文件,您可以获取
它们在这里:

http://svn.effbot.org/public/pil/Scripts/


player.py 从命令行运行,

看看这个是否适合您:

from Tkinter import * 
from PIL import Image, ImageTk


class MyLabel(Label):
    def __init__(self, master, filename):
        im = Image.open(filename)
        seq =  []
        try:
            while 1:
                seq.append(im.copy())
                im.seek(len(seq)) # skip to next frame
        except EOFError:
            pass # we're done

        try:
            self.delay = im.info['duration']
        except KeyError:
            self.delay = 100

        first = seq[0].convert('RGBA')
        self.frames = [ImageTk.PhotoImage(first)]

        Label.__init__(self, master, image=self.frames[0])

        temp = seq[0]
        for image in seq[1:]:
            temp.paste(image)
            frame = temp.convert('RGBA')
            self.frames.append(ImageTk.PhotoImage(frame))

        self.idx = 0

        self.cancel = self.after(self.delay, self.play)

    def play(self):
        self.config(image=self.frames[self.idx])
        self.idx += 1
        if self.idx == len(self.frames):
            self.idx = 0
        self.cancel = self.after(self.delay, self.play)        


root = Tk()
anim = MyLabel(root, 'animated.gif')
anim.pack()

def stop_it():
    anim.after_cancel(anim.cancel)

Button(root, text='stop', command=stop_it).pack()

root.mainloop()

Newsgroups: comp.lang.python

From: "Fredrik Lundh"

Date: Mon, 1 May 2006

Daniel Nogradi wrote:

'The source distribution of the 1.1.4 version comes with a Scripts
directory where you can find player.py, gifmaker.py and explode.py
which all deal with animated gif.'

they're still shipped with 1.1.5 (and 1.1.6), and they should work.

if all you're missing is a few files from the script directory, you can get
them here:

http://svn.effbot.org/public/pil/Scripts/


player.py is run from the command line

see if this one works for you:

from Tkinter import * 
from PIL import Image, ImageTk


class MyLabel(Label):
    def __init__(self, master, filename):
        im = Image.open(filename)
        seq =  []
        try:
            while 1:
                seq.append(im.copy())
                im.seek(len(seq)) # skip to next frame
        except EOFError:
            pass # we're done

        try:
            self.delay = im.info['duration']
        except KeyError:
            self.delay = 100

        first = seq[0].convert('RGBA')
        self.frames = [ImageTk.PhotoImage(first)]

        Label.__init__(self, master, image=self.frames[0])

        temp = seq[0]
        for image in seq[1:]:
            temp.paste(image)
            frame = temp.convert('RGBA')
            self.frames.append(ImageTk.PhotoImage(frame))

        self.idx = 0

        self.cancel = self.after(self.delay, self.play)

    def play(self):
        self.config(image=self.frames[self.idx])
        self.idx += 1
        if self.idx == len(self.frames):
            self.idx = 0
        self.cancel = self.after(self.delay, self.play)        


root = Tk()
anim = MyLabel(root, 'animated.gif')
anim.pack()

def stop_it():
    anim.after_cancel(anim.cancel)

Button(root, text='stop', command=stop_it).pack()

root.mainloop()
一梦等七年七年为一梦 2024-12-20 09:03:34

简单的PIL版本:

canvas = Image.new("RGB",(Width,Height),"white")
gif = Image.open('text.gif', 'r')
frames = []
try:
    while 1:
        frames.append(gif.copy())
        gif.seek(len(frames))
except EOFError:
    pass

for frame in frames:
     canvas.paste(frame)
     canvas.show()

Simple PIL version:

canvas = Image.new("RGB",(Width,Height),"white")
gif = Image.open('text.gif', 'r')
frames = []
try:
    while 1:
        frames.append(gif.copy())
        gif.seek(len(frames))
except EOFError:
    pass

for frame in frames:
     canvas.paste(frame)
     canvas.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文