如何在 Linux 下显示 gif 动画?

发布于 2024-12-29 06:28:48 字数 615 浏览 6 评论 0原文

我想从 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 技术交流群。

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

发布评论

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

评论(2

茶花眉 2025-01-05 06:28:48

Image.show 将图像转储到临时文件,然后尝试显示该文件。它调用 ImageShow.Viewer.show_image (参见 /usr/lib/python2.7/dist-packages/PIL/ImageShow.py):

class Viewer:
    def save_image(self, image):
        # save to temporary file, and return filename
        return image._dump(format=self.get_format(image))
    def show_image(self, image, **options):
        # display given image
        return self.show_file(self.save_image(image), **options)
    def show_file(self, file, **options):
        # display given file
        os.system(self.get_command(file, **options))
        return 1

AFAIK,标准 PIL 无法保存动画 GIf1

Viewer.save_image 中的 image._dump 调用仅保存第一帧。因此,无论随后调用什么查看器,您都只能看到静态图像。

如果您有 Imagemagick 的 display 程序,那么您也应该有它的 animate 程序。因此,如果您已经将 GIF 作为文件,那么您可以使用

animate /path/to/animated.gif

要在 Python 中执行此操作,您可以使用 subprocess 模块(而不是 img.show):

import subprocess

proc = subprocess.Popen(['animate', '/path/to/animated.gif'])
proc.communicate()

1 根据kostmo,有一个用PIL保存动画GIFS的脚本。


要在不阻塞主进程的情况下显示动画,请使用单独的线程生成 animate 命令:

import subprocess
import threading

def worker():
    proc = subprocess.Popen(['animate', '/path/to/animated.gif'])
    proc.communicate()

t = threading.Thread(target = worker)
t.daemon = True
t.start()
# do other stuff in main process
t.join()

Image.show dumps the image to a temporary file and then tries to display the file. It calls ImageShow.Viewer.show_image (see /usr/lib/python2.7/dist-packages/PIL/ImageShow.py):

class Viewer:
    def save_image(self, image):
        # save to temporary file, and return filename
        return image._dump(format=self.get_format(image))
    def show_image(self, image, **options):
        # display given image
        return self.show_file(self.save_image(image), **options)
    def show_file(self, file, **options):
        # display given file
        os.system(self.get_command(file, **options))
        return 1

AFAIK, the standard PIL can not save animated GIfs1.

The image._dump call in Viewer.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 its animate program. So if you have the GIF as a file already, then you could use

animate /path/to/animated.gif

To do so from within Python, you could use the subprocess module (instead of img.show):

import subprocess

proc = subprocess.Popen(['animate', '/path/to/animated.gif'])
proc.communicate()

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:

import subprocess
import threading

def worker():
    proc = subprocess.Popen(['animate', '/path/to/animated.gif'])
    proc.communicate()

t = threading.Thread(target = worker)
t.daemon = True
t.start()
# do other stuff in main process
t.join()
战皆罪 2025-01-05 06:28:48

linux 打开 gif

我在 Fedora 17 中执行此操作:

el@defiant ~ $ sudo yum install gifview
---> Package gifview.x86_64 0:1.67-1.fc17 will be installed

curl http://i.imgur.com/4rBHtSm.gif > mycat.gif

gifview mycat.gif

出现一个窗口,您可以单步浏览 gif 的帧。

linux open a gif

I did this with Fedora 17:

el@defiant ~ $ sudo yum install gifview
---> Package gifview.x86_64 0:1.67-1.fc17 will be installed

curl http://i.imgur.com/4rBHtSm.gif > mycat.gif

gifview mycat.gif

A window comes up and you can step through the frames of the gif.

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