How to make movie from images in python

I am currently trying to make a movie out of images, but I have not found anything useful.

Here is my code:

import time

from PIL import  ImageGrab

x =0

while True:
    try:
        x+= 1
        ImageGrab().grab().save('img{}.png'.format(str(x))
    except:
        movie = #Idontknow
        for _ in range(x):
            movie.save("img{}.png".format(str(_)))

movie.save()
+4
source share
2 answers

You can use an external tool, such as ffmpeg, to merge images into a movie (see answer here ), or you can try using OpenCv to merge images into a movie, similar to the example here .

I am attaching below code. I used to combine all png files from a folder named "images" in the video.

import cv2
import os

image_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.shape

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

for image in images:
    video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()
+13
source

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")

:)

+7

Source: https://habr.com/ru/post/1680877/


All Articles