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?
Image.showdumps 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._dumpcall inViewer.save_imageonly saves the first frame. So no matter what viewer is subsequently called, you only see a static image.If you have Imagemagick’s
displayprogram, then you should also have itsanimateprogram. 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
animatecommand: