How to rewind a animated GIF picture in Python _python

Source: Internet
Author: User
Tags rewind in python

This time, let's use Python as a gadget: Move the animated GIF picture Backwards!

GIF (Graphics Interchange Format) is a picture format that you can use to render animation effects by saving many frame-static images and then rendering them sequentially. Many short videos will also be converted to animated GIF rendering, compressing the image and removing the sound to effectively reduce the file size. Countless funny moves on the network, almost bearing the joy of most of the netizens, but some people found that the normal animation in reverse can often get more funny effect, Reddit even have a special node:/r/reversegif.

To do this is very simple, as long as you extract the middle of each frame from the original picture static images, and then the order is reversed and then regenerate a GIF image. The library dedicated to working with images in Python is PiL, and a more user-friendly version was developed on PIL basis by Alex Clark and others: Pillow. The first step is to install (or upgrade) Pillow:

Pip install--upgrade pillow-i http://pypi.douban.com/simple
python-c "import pil; Print (PIL. VERSION, ' \ t ', PiL. pillow_version) "
//1.1.7 3.4.2
use PILLOW to read the picture file and determine whether the picture is a dynamic picture and its number of frames: from
pil import imagewith (' ani.gif ') as im:
print (im.is_animated, im.n_frames)

The new version of Pillow provides more functionality for writing to GIF:

Im.save (out, Save_all=true, append_images=[im1, IM2, ...])

By setting Save_all=true parameters and Append_images, you can save multiple-frame images into GIF images at once, so that we can generate images that play in reverse order:

From PIL import Image, imagesequence with
image.open (' ani.gif ') as im:
if im.is_animated:
frames = [F.copy () For f in Imagesequence.iterator (IM)]
frames.reverse () # Built-in list reverse Method
# saves all frame images in reverse sequence
frames[0].save (' Out.gif ', Save_all=true, append_images=frames[1:]

Let's look at a more obvious effect:


In order to meet the micro-letter 2M picture size limit, the reverse image after the sample compression processing

If you want to achieve some of the effects of ghost animals, you can also randomly upset all the frames order:

Import Random
# frames.reverse ()
random.shuffle (frames)

The effect is as follows:

The above is a small set to the introduction of Python will be animated GIF image inverted playback method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.