python tkinter 使用 PIL 显示动画 GIF [英] python tkinter display animated GIF using PIL

查看:75
本文介绍了python tkinter 使用 PIL 显示动画 GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我认为 ImageSequence 模块 将是方法它,但我不知道如何使用它以及是否可能.

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.

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

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.

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

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?

第二个问题:即使我必须创建一个函数来循环遍历 GIF 帧,ImageSequence 模块应该这样做还是 PIL 有另一个模块?

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?

我使用的是 Python 3.1 和一个 PIL 的私有端口,在这个主题中指出.

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

推荐答案

新闻组:comp.lang.python

Newsgroups: comp.lang.python

来自:弗雷德里克·伦德"

From: "Fredrik Lundh"

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

Date: Mon, 1 May 2006

丹尼尔·诺格拉迪写道:

Daniel Nogradi wrote:

'1.1.4版本的源码分发自带了一个Scripts您可以在其中找到 player.py、gifmaker.py 和 expand.py 的目录所有这些都处理动画 gif.'

'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.'

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

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 从命令行运行

player.py is run from the command line

看看这个是否适合你:

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()

这篇关于python tkinter 使用 PIL 显示动画 GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆