In this tutorial, we will see how to create animated gifs in real-time with Opencv and Python. The abbreviation GIF stands for Graphic Interchange Format, it is a particular type of image that contains animations. has become very popular because it requires little memory and allows you to show a very short video. You will learn how to do this using a camera or from a video.

Libraries Installation

To do this you will only need two libraries and a few lines of code. If you haven’t already, you need to install OpenCV and imageio

Install OpenCV

The installation can be done with a single line on practically all operating systems but if you need more details on this topic I recommend How to install Python 3 and Opencv 4 on Windows

pip install opencv-python

Install imageio

For more details on the imageio library, I recommend visiting the site with the documentation. In this step, we will focus only on the installation.

pip install imageio

Create an animated GIF in real-time with Opencv and Python

For convenience, we divide the operation into two main steps:

  1. Collect the images with OpenCV
  2. Use images to create animation

1. Collect the images with OpenCV

There are several ways to take multiple images, in my case I will take them from the webcam in real time. I enter the code to identify the webcam and set 0 if I only have one webcam connected, 1 if I want to show the second webcam etc.

import cv2
import imageio

# 1. Get Images
cap = cv2.VideoCapture(0)

if everything works correctly I take the frame that the webcam is generating. Remember to insert it inside a While loop to make sure that the flow is continuous.

while True:
    ret, frame = cap.read()
    cv2.imshow("frame", frame)

Now we just have to save the images. It can be done simply by putting the images into an array, in my case frames []. Furthermore, to decide when to save the frame, I set a command connected to the keyboard, just like a camera.

  • If I press the letter a, I save the image
  • If I press the letter q I finish the operation

This is the code:

frames = []
image_count = 0
while True:
    ret, frame = cap.read()

    cv2.imshow("frame", frame)

    key = cv2.waitKey(0)
    if key == ord("a"):
        image_count += 1
        frames.append(frame)
        print("Adding new image:", image_count)
    elif key == ord("q"):
        break

print("Images added: ", len(frames))

2. Use images to create animations

Now we need to save the images stored in the array and turn them into a gif file. Obviously, we will use the imageio library for this.

We use the get_writer() function, we must set two parameters (filename, mode = “I”), the letter I is used to tell the library to save a sequence of images. Let’s see the code:

print("Saving GIF file")
with imageio.get_writer("smiling.gif", mode="I") as writer:
    for idx, frame in enumerate(frames):
        print("Adding frame to GIF file: ", idx + 1)
        writer.append_data(frame)

but we have a problem. As you can see from the image, the colors tend towards blue

This happens because the libraries use a different format for the images. OpenCV uses the BGR format instead imageio uses the RGB format. The problem is easily solved with a line of conversion code.

rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

This is the final result