Creating a video with a set of images using Python

Strongly suggest you to get your own portfolio site and show us :)

We can create videos with the pictures present using the OpenCv Library.
OpenCv is an open-source image processing library which contains various predefined methods for performing tasks related to Computer Vision,Machine Learning.

Inorder to create a video out of the images we are using Opencv & PIL libraries. PIL stands for Python Imaging Library which is used to resize the images to the average height so that a video can be created using OpenCv.

The below piece of code does the following

  • Opens a directory which contains the images
  • Iterates through all the images and calculates the mean-height and mean-width
  • Resizes the images to the mean-height and mean-width
  • Save the image back into the same directory
import os
import cv2
from PIL import Image
os.chdir("/home/ganesh/Desktop/video")
total_width=0
total_height=0
for file in os.listdir('.'):
    im=Image.open(os.path.join("/home/ganesh/Desktop/video",file))
    width,height=im.size
    total_width+=width
    total_height+=height
numofimages=len(os.listdir('.'))
mean_width=int(total_width/numofimages)  #calculating the width for each image
mean_height=int(total_height/numofimages)  #calculating the height for each image 
for file in os.listdir('.'):
    if file.endswith(".jpg") or file.endswith(".jpeg") or file.endswith(".png"):
        im=Image.open(os.path.join("/home/ganesh/Desktop/video",file))
        imResize=im.resize((mean_width,mean_height),Image.ANTIALIAS)
        imResize.save(file,'JPEG',quality=95)

  • os.chdir():
    It is used to change the current working directory to the directory specified as parameter.

  • os.listdir():
    It is used to list all the contents present in the given directory.

  • Image.open():
    It is used to open the image in the path given as parameter.

  • file.endswith():
    It returns true if the file name ends with the given suffix otherwise returns false.

  • im.resize():
    It returns the resized image.It takes tuple which contains the height and width to which the image must be resized and also an optional parameter which is used as resampling filter.

  • im.save():
    It is used to save the image to the required file extension and quality.

Generating a Video with the Images:

The below function generate_video() is used to make a video with all the images present in the directory.

def generate_video():
    image_folder = '.' # Use the folder
    video_name = 'mygeneratedvideo.avi'
    os.chdir("/home/ganesh/Desktop/video")
    images = [img for img in os.listdir(image_folder) if img.endswith(".jpg") or
             img.endswith(".jpeg") or img.endswith("png")]
   
    fourcc = cv2.VideoWriter_fourcc(*'DIVX') 

    # Array images should only consider 
    # the image files ignoring others if any 
    
    frame = cv2.imread(os.path.join(image_folder, images[0]))
    
    # setting the frame width, height width 
    # the width, height of first image 
    height, width, layers = frame.shape

    video = cv2.VideoWriter(video_name, fourcc, 1, (width, height))

    # Appending the images to the video one by one 
    for image in images:
        video.write(cv2.imread(os.path.join(image_folder,image)))
        
    # Deallocating memories taken for window creation 
    cv2.destroyAllWindows()
    video.release()  # releasing the video generated 
    
generate_video()
  • cv2.imread():
    This function is used to read the image at specific location. It takes the absolute path of the image as parameter. It also takes an optional parameter flag which is used to read RGB or GREY Scale image.

  • cv2.VideoWriter():
    It is used to write the frames to the video file specified. It takes various parameters like frames_per_second,frame_size and an optional parameter isColor.Here FourCC is a 4-byte code used to specify the video codec. It is platform dependent.

  • VideoWriter.write():
    This function is used to write the next video frame. It takes the image as parameter and writes it to the video.

  • VideoWriter.release():
    It is used to close the currently opened file.

Note :
Every video represents set of images that are shown at a certain speed called as frames.This speed is said to be Frames Per Second(fps).It decides the duration of the total video.The duration of the video can be calculated by following formula:

Duration_Of_Video = Number_Of_Frames/Frames_Per_Second

The above formula determines that the Frames_Per_Second(fps) is inversely proportion to the Duration_Of_Video i.e less fps increases the duration and more fps decrease duration.The duration of video can be calculated using Opencv as following:

import cv2
cap = cv2.VideoCapture("./video.mp4")  
fps = cap.get(cv2.CAP_PROP_FPS)   #frames per second
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))   #total number of frames
duration = frame_count/fps

Question

What does the image.shape return for colored images and greyscale images?

(rows,columns,channels) (rows,columns)
[rows,columns,channels] [rows,columns]
[rows,columns] [rows,columns]
None of the above
If an image is grayscale, the tuple returned contains only the number of rows and columns and for colour images it contains all 3, so we must always check whether the loaded image is grayscale or color.