Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading