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