Animate image using OpenCV in Python


Introduction

Animated pictures are a sequence of static images that are automatically played to display relevant material in a continuous and dynamic manner, and they have the benefit of being smaller than videos. Additionally, many online and mobile applications enable animated images in the same manner that they support static photos, although they do not always permit the upload of movies. In this situation, animated pictures can be really helpful. Clearly, there are several other reasons why animated graphics are preferable. Sometimes, combining similar or unrelated photos is simply for fun.

In this article, we would learn the steps to make animated images in Python OpenCV.

OpenCV

Gary Bradsky initiated OpenCV in 1999 at Intel, and the initial release wasn't made available until the year 2000. Vadim Pisarevsky is now working with Gary Bradsky to supervise the OpenCV team for Intel's Russian software. OpenCV was used in Stanley, the vehicle that triumphed in the 2005 DARPA Grand Challenge, and was named the winner. After then, its productive growth carried on with the assistance of Willow Garage, and Gary Bradsky and Vadim Pisarevsky were the ones in charge of the project. OpenCV today supports a wide variety of methods that are associated with computer vision and machine learning, and its functionality is continuing to grow on a daily basis.

OpenCV is accessible on a number of platforms, including Windows, Linux, OS X, Android, and iOS, and it supports a broad range of programming languages, including C++, Python, Java, and others. Additionally, the development of interfaces for high-speed GPU operations that are based on CUDA and OpenCL is now taking place.

It provides extensive support for all languages, including Python, C++, Android, Java, and so on. It is simple to use and popular owing to the characteristics that it has. It is used in the process of developing image processing or rendering applications by using a variety of programming languages.

Using the CV2 version of OpenCV, we will attempt to execute certain picture transformations during the course of this tutorial.So let’s get started.

Installation Requirements

The code requires a working installation of Python 3 with OpenCV and its dependency NumPy.

We need to have an environment to run the program we can use Jupyter Notebook to execute our program. Here we have used OpenCV for image animation but there are many more algorithms using which we can animate the image or we can make a gif also.

How to animate images using OpenCV?

Animated pictures are a sequence of static images that are automatically played to display relevant animation. The hstack() method included in the NumPy package will be used to concatenate the two pictures that we have. The hstack function is used to stack the sequence of input arrays horizontally (that is, column-wise), resulting in a single array. As parameters, the hstack function expects a tuple that contains the order of the arrays.

We have imported the image from google drive by mounting the drive in our program and setting the path of the image.

Example

from google.colab import drive
drive.mount('/content/drive')

Output

Mounted at /content/drive

Fetching the path

path='/content/drive/MyDrive/Doremon.jpeg'

import cv2
import numpy as npp

from google.colab.patches import cv2_imshow
# Load the image
img1 = cv2.imread(path)
h, w, c = img1.shape


# Loop for a specified number of frames
for i in range(100):
   # Update the image
   ll = img1[:, :(i % w)]
   rr = img1[:, (i % w):]
  
   img2 = npp.hstack((rr, ll))
      
   # Display the updated image
   cv2.imshow( 'animation',img2)

   # Wait for a specified amount of time
   cv2.waitKey(100)

# Close the window
cv2.destroyAllWindows()

Conclusion

We learned the very fundamental procedures that are required to start the picture animation by utilizing OpenCV and a few lines of Python.

The value of being able to write images using code obviously extends much beyond just being able to write random things. For example, each frame can have been read from a storage location using cv.imread, or it might have been captured directly from a camera using cv2.VideoCapture. Before adding each frame to the movie, we have the ability to conduct one or more distinct operations on that frame, such as color conversions, finding contours, morphological changes, and many others.

Updated on: 24-Aug-2023

483 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements