How to make a movie out of images in python

前端 未结 4 1893
野性不改
野性不改 2020-12-02 11:30

I currently try to make a movie out of images, but i could not find anything helpful .

Here is my code so far:

import time

from PIL import  ImageGra         


        
相关标签:
4条回答
  • 2020-12-02 11:40

    I use the ffmpeg-python binding. You can find more information here.

    import ffmpeg
    (
        ffmpeg
        .input('/path/to/jpegs/*.jpg', pattern_type='glob', framerate=25)
        .output('movie.mp4')
        .run()
    )
    
    0 讨论(0)
  • 2020-12-02 11:44

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

    0 讨论(0)
  • 2020-12-02 11:46

    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 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, 0, 1, (width,height))
    
    for image in images:
        video.write(cv2.imread(os.path.join(image_folder, image)))
    
    cv2.destroyAllWindows()
    video.release()
    
    0 讨论(0)
  • 2020-12-02 11:58

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

    import os
    import moviepy.video.io.ImageSequenceClip
    image_folder='folder_with_images'
    fps=1
    
    image_files = [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')
    
    0 讨论(0)
提交回复
热议问题