Categories
Python Answers

How to programmatically generate video or animated GIF in Python?

Spread the love

Sometimes, we want to programmatically generate video or animated GIF in Python.

In this article, we’ll look at how to programmatically generate video or animated GIF in Python.

How to programmatically generate video or animated GIF in Python?

To programmatically generate video or animated GIF in Python, we can use the imageio library.

To install it, we run

pip install imageio

Then we can use it by writing

import imageio
with imageio.get_writer('/path/to/movie.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)

to create an animated GIF and save it to /path/to/movie.gif with imageio.getwriter.

In the with block, we loop through the filenames list to add the images at filename into the animated GIF with

image = imageio.imread(filename)
writer.append_data(image)

We read the image with imageio.imread and call append_data to append the image as a frame in the animated GIF.

Conclusion

To programmatically generate video or animated GIF in Python, we can use the imageio library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *