How to display animated gif from Linux?

I want to open a GIF image from the python console on Linux. Normally when opening .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 show only the first frame of the gif, not the animation.

Is there a way to show GIF animations in a viewer on Linux?

+8
python linux gif python-imaging-library animated-gif
source share
2 answers

Image.show deletes the image in 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 cannot save animated GIfs 1 .

The call to image._dump in Viewer.save_image saves only the first frame. Therefore, no matter which viewer is subsequently called up, you only see a static image.

If you have Imagemagick display , you should also have your animate program. Therefore, if you already have a GIF file, you can use

 animate /path/to/animated.gif 

To do this from Python, you can 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 for saving animated GIFs with PIL.


To show animation without blocking the main process, use a separate thread to invoke 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() 
+5
source share

linux open 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 will appear and you can go through the gif frames.

+1
source share

All Articles