How to create gif animation with Python

Python Gif Animation

Today, I came across a python image library imageio that helps to read and write a wide range of image data, including animated images, volumetric data, and scientific formats.

Required Packages

Install imageio via pip

## Creating gif using imageio
import sys
import datetime
import imageio

VALID_EXTENSIONS = ('png', 'jpg')

def create_gif(filenames, duration):
    images = []
    for filename in filenames:
    output_file = 'Gif-%s.gif' %'%Y-%M-%d-%H-%M-%S')
    imageio.mimsave(output_file, images, duration=duration)

if __name__ == "__main__":
    script = sys.argv.pop(0)

    if len(sys.argv) < 2:
        print('Usage: python {} <duration> <path to images separated by space>'.format(script))

    duration = float(sys.argv.pop(0))
    filenames = sys.argv

    if not all(f.lower().endswith(VALID_EXTENSIONS) for f in filenames):
        print('Only png and jpg files allowed')

    create_gif(filenames, duration)

How to use ?

The program takes minimum two arguments duration of the gif and image need to created as gif.

python 2 1.png 2.png 3.png