如何在 Linux 下显示 gif 动画?
我想从 Linux 中的 python 控制台打开 GIF 图像。通常,当打开 .png
或 .jpg
时,我会执行以下操作:
>>> from PIL import Image
>>> img = Image.open('test.png')
>>> img.show()
但如果我这样做:
>>> from PIL import Image
>>> img = Image.open('animation.gif')
>>> img.show()
Imagemagick 将打开,但仅显示 gif 的第一帧,而不是动画。
有没有办法在 Linux 的查看器中显示 GIF 动画?
I want to open a GIF image from the python console in Linux. Normally when opening a .png
or .jpg
, I would do the following:
>>> from PIL import Image
>>> img = Image.open('test.png')
>>> img.show()
But if I do this:
>>> from PIL import Image
>>> img = Image.open('animation.gif')
>>> img.show()
Imagemagick will open but only show the first frame of the gif, not the animation.
Is there a way to show the animation of the GIF in a viewer in Linux?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Image.show
将图像转储到临时文件,然后尝试显示该文件。它调用ImageShow.Viewer.show_image
(参见/usr/lib/python2.7/dist-packages/PIL/ImageShow.py
):AFAIK,标准 PIL 无法保存动画 GIf1。
Viewer.save_image
中的image._dump
调用仅保存第一帧。因此,无论随后调用什么查看器,您都只能看到静态图像。如果您有 Imagemagick 的
display
程序,那么您也应该有它的animate
程序。因此,如果您已经将 GIF 作为文件,那么您可以使用要在 Python 中执行此操作,您可以使用 subprocess 模块(而不是
img.show
):1 根据kostmo,有一个用PIL保存动画GIFS的脚本。
要在不阻塞主进程的情况下显示动画,请使用单独的线程生成
animate
命令:Image.show
dumps the image to a temporary file and then tries to display the file. It callsImageShow.Viewer.show_image
(see/usr/lib/python2.7/dist-packages/PIL/ImageShow.py
):AFAIK, the standard PIL can not save animated GIfs1.
The
image._dump
call inViewer.save_image
only saves the first frame. So no matter what viewer is subsequently called, you only see a static image.If you have Imagemagick's
display
program, then you should also have itsanimate
program. So if you have the GIF as a file already, then you could useTo do so from within Python, you could use the subprocess module (instead of
img.show
):1 According to kostmo, there is a script to save animated GIFS with PIL.
To show the animation without blocking the main process, use a separate thread to spawn the
animate
command:linux 打开 gif
我在 Fedora 17 中执行此操作:
出现一个窗口,您可以单步浏览 gif 的帧。
linux open a gif
I did this with Fedora 17:
A window comes up and you can step through the frames of the gif.