Use Python to reverse playback of dynamic GIF images,

Source: Internet
Author: User

Use Python to reverse playback of dynamic GIF images,

This time, let's make a small tool using Python: Playing dynamic GIF images in reverse order!

GIF (Graphics Interchange Format) is an image Format that can be used to present animation effects. The principle is to save a lot of static frames and then present them continuously. Many short videos will also be converted to dynamic GIF Rendering. After compressing the image quality and removing the sound, the file size can be effectively reduced. There are countless funny animations on the Internet, which almost carry the joy of most of the netizens. However, some people also find that playing normal animations in reverse order can often achieve more funny effects, there is even a dedicated node on Reddit:/r/reversegif.

To do this, it is actually very simple. You only need to extract the static image of each frame from the original image, and then re-generate a GIF image after reversing the sequence. Python uses PIL as the library for image processing. Based on PIL, Alex Clark and others developed a more friendly version: Pillow. So first install (or upgrade) Pillow:

Pip install -- upgrade pillow-I http://pypi.douban.com/simplepython-c "import PIL; print (PIL. VERSION, '\ t', PIL. PILLOW_VERSION) "// 1.1.7 3.4.2 using Pillow, you can read the image file and determine whether the image is a dynamic image and its frames: from PIL import Imagewith Image.open('ani.gif ') as im: print (im. is_animated, im. n_frames)

The new version of Pillow provides more GIF writing functions:

im.save(out, save_all=True, append_images=[im1, im2, ...])

By setting the save_all = True parameter and append_images, you can save multiple frames to the GIF image at the same time, so we can generate an inverted playback image like this:

From PIL import Image, ImageSequencewith Image.open('ani.gif ') as im: if im. is_animated: frames = [f. copy () for f in ImageSequence. iterator (im)] frames. reverse () # built-in inverted list method # Save all frames behind the inverted list to frames%0%.save('out.gif ', save_all = True, append_images = frames [1:])

Let's take a look at the obvious effect:


In order to meet the size limit of 2 MB images, inverted images are sampled and compressed.

If you want to achieve some results, you can also randomly disrupt the order of all frames:

import random# frames.reverse()random.shuffle(frames)

The effect is as follows:

The above section describes how to use Python to reverse playback of dynamic GIF images. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.