Quantcast
Channel: Idiot Inside
Viewing all articles
Browse latest Browse all 26

How to create gif animation with Python

$
0
0

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 pip install imageio

Creating gif using imageio

#gif.py 
import sys
import datetime
import imageio

VALID_EXTENSIONS = ('png', 'jpg')


def create_gif(filenames, duration):
    images = []
    for filename in filenames:
        images.append(imageio.imread(filename))
    output_file = 'Gif-%s.gif' % datetime.datetime.now().strftime('%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))
        sys.exit(1)

    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')
        sys.exit(1)

    create_gif(filenames, duration)

How to use ?

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

python gif.py 2 1.png 2.png 3.png

Viewing all articles
Browse latest Browse all 26

Trending Articles