加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何用Linux显示动画gif?

发布时间:2020-12-13 16:58:47 所属栏目:Linux 来源:网络整理
导读:我想从 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() Imagema
我想从 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的动画?

解决方法

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 can not save animated GIfs1.

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

如果你有Imagemagick的显示程序,那么你也应该有它的动画程序.因此,如果您已将GIF作为文件,那么您可以使用

animate /path/to/animated.gif

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

import subprocess

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

1 According to 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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读