python tkinter使用PIL显示动画GIF

编程入门 行业动态 更新时间:2024-10-24 04:47:05
本文介绍了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

来源:"Fredrik Lundh"

From: "Fredrik Lundh"

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

Date: Mon, 1 May 2006

Daniel Nogradi写道:

Daniel Nogradi wrote:

'1.1.4版本的源代码发行版附带了一个脚本 您可以在其中找到player.py,gifmaker.py和explode.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:

svn.effbot/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

本文发布于:2023-06-08 06:32:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/576167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动画   tkinter   python   GIF   PIL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!