user19862793
user19862793

Reputation: 171

Making a movie from number of images in Python

I am making a movie from 10 images. However, I don't want to enter the file name for each in files_and_duration. Instead, I want to write a one-line command which reads all the 10 files together.

import cv2

frame_per_second = 1

files_and_duration = [
    ('1.png', 1),
    ('2.png', 1),
    ('3.png', 1),
    ('4.png', 1),
    ('5.png', 1),
    ('6.png', 1),
    ('7.png', 1),
    ('8.png', 1),
    ('9.png', 1),
    ('10.png', 1)]

w, h = None, None
for file, duration in files_and_duration:
    frame = cv2.imread(file)

    if w is None:
        
        h, w, _ = frame.shape
        fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
        writer = cv2.VideoWriter('output1.mp4', fourcc, frame_per_second, (w, h))


    for repeat in range(duration * frame_per_second):
        writer.write(frame)

writer.release()

Upvotes: 0

Views: 373

Answers (1)

İlker Kara
İlker Kara

Reputation: 161

import glob
files = glob.glob('*.png')
files_and_duration = [(i,frame_per_second) for i in files]

You can use glob library to find all '.png' files in your directory.

Upvotes: 1

Related Questions