Home Developer Programming Language Framework Articles Contact Me

HTML CSS JAVASCRIPT JAVA PYTHON JQUERY REACTJS VUEJS ANGULARJS EMBERJS SASS LESS STYLUS GITHUB GITLAB BIGBUCKET JAVA KOTLIN HADOOP JDBC SERVLETS JAVA BEANS(EJB) JAVA SERVER PAGES(JSP) UNIX LINUX POSTGRESQL MSSQLSERVER SQLITE MONGODB XML FLASK PYTHON MYSQL JAVA C++ JUNIT SELENIUM

How to Convert Video to GIF Using Python

In this article, we will learn how we can convert a video into GIF Using Python.

What is GIF?

In Simple words, a GIF is a collection of Images that is similar to a video, or we can say GIF is a small part of the video.

Libraries Required:-

  1. OpenCV
  2. OS (Comes with Python)
  3. Pillow
  4. glob

Installation:-

pip install opencv-python
pip install Pillow
pip install glob2

Approach:-

  1. First, we convert video into images
  2. Then we convert all images into one GIF

Let's Understand Step by Step Implementation:-

Step 1 (Import Required Library)

# Import Required Module

import cv2 
import os 
from PIL import Image
import glob

Step 2 (Convert Video Into images)

Now, we will create a function named convert_video_into_images which is used to convert a video to images.

def convert_video_into_images(video_path):

    # Read the video from specified path 
    cam = cv2.VideoCapture(video_path) 

    # set current frame to zero 
    currentframe = 0

    # Infinite loop
    while(True): 
        # reading each frame 
        ret,frame = cam.read() 

        if ret: 
            name = 'frame' + str(currentframe) + '.png'
            
            # writing the extracted images 
            cv2.imwrite(name, frame) 

            # Increase the current frame value 
            currentframe += 1
        else: 
            break

    cam.release() 
    cv2.destroyAllWindows()
  

Step 3 (Convert All Images Into one GIF)

def images_to_gif():
    # Create the frames
    frames = []
    imgs = glob.glob("*.png")
    for i in imgs:
        new_frame = Image.open(i)
        frames.append(new_frame)
     
    # Save into a GIF file that loops forever
    frames[0].save('file.gif', format='GIF',
                   append_images=frames[1:],
                   save_all=True,
                   duration=300, loop=0)
  

Step 3 (Delete All Images)

In this function, we will delete all images that we have created from the first function (convert_video_into_images)

def delete_all_images():
    for i in os.listdir():
      if ".png" in i:
          os.remove(i)

Below is the Implementation:-

# Import Required Module
import cv2 
import os 
from PIL import Image
import glob


def convert_video_into_images(video_path):
    # Read the video from specified path 
    camera = cv2.VideoCapture(video_path) 

    # set current frame to zero 
    current_frame = 0

    # Infinite loop
    while(True): 
        # reading each frame 
        ret,frame = camera.read() 

        if ret: 
            file_name = 'frame' + str(current_frame) + '.png'
            
            # writing the extracted images 
            cv2.imwrite(file_name, frame) 

            # Increase the current frame value 
            current_frame += 1
        else: 
            break

    camera.release() 
    cv2.destroyAllWindows() 

 
def images_to_gif():
    # Frame List
    frames = []

    images = glob.glob("*.png")

    for image in images:
        new_frame = Image.open(image)
        frames.append(new_frame)
     
    # Save into a GIF file that loops forever
    frames[0].save('abh.gif', format='GIF',
                   append_images=frames[1:],
                   save_all=True,
                   duration=300, loop=0)

def delete_all_images():
    # Iterate through all images
    for i in os.listdir():
        if ".png" in i:
            # remove file
            os.remove(i)


if __name__ == '__main__':
    convert_video_into_images("Enter Video Path")
    images_to_gif()
    delete_all_images()

Input:-


Output:-