How to make a movie out of images in python How to make a movie out of images in python python python

How to make a movie out of images in python


You could consider using an external tool like ffmpeg to merge the images into a movie (see answer here) or you could try to use OpenCv to combine the images into a movie like the example here.

I'm attaching below a code snipped I used to combine all png files from a folder called "images" into a video.

import cv2import osimage_folder = 'images'video_name = 'video.avi'images = [img for img in os.listdir(image_folder) if img.endswith(".png")]frame = cv2.imread(os.path.join(image_folder, images[0]))height, width, layers = frame.shapevideo = cv2.VideoWriter(video_name, 0, 1, (width,height))for image in images:    video.write(cv2.imread(os.path.join(image_folder, image)))cv2.destroyAllWindows()video.release()

It seems that the most commented section of this answer is the use of VideoWriter. You can look up it's documentation in the link of this answer (static) or you can do a bit of digging of your own. The first parameter is the filename, followed by an integer (fourcc in the documentation, the codec used), the FPS count and a tuple of the dimensions of the frame. If you really like digging in that can of worms, here's the fourcc video codecs list.


Thanks , but i found an alternative solution using ffmpeg:

def save():    os.system("ffmpeg -r 1 -i img%01d.png -vcodec mpeg4 -y movie.mp4")

But thank you for your help :)


Here is a minimal example using moviepy. For me this was the easiest solution.

import osimport moviepy.video.io.ImageSequenceClipimage_folder='folder_with_images'fps=1image_files = [os.path.join(image_folder,img)               for img in os.listdir(image_folder)               if img.endswith(".png")]clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)clip.write_videofile('my_video.mp4')