Python JPEG to movie

前端 未结 4 1121
再見小時候
再見小時候 2020-12-29 10:20

I am looking for a way to concatenate a directory of images files (e.g., JPEGs) to a movie file (MOV, MP4, AVI) with Python. Ideally, this would also allow me to take multip

相关标签:
4条回答
  • 2020-12-29 10:21

    I finally got into a working version of the project that got me into this question. Now I want to contribute with the knowledge I got. Here is my solution for getting all pictures in current directory and converting into a video having then centralized in a black background, so this solution works for different size images.

    import glob
    import cv2
    import numpy as np
    
    DESIRED_SIZE = (800, 600)
    SLIDE_TIME = 5 # Seconds each image
    FPS = 24
    
    fourcc = cv2.VideoWriter.fourcc(*'X264')
    writer = cv2.VideoWriter('output.avi', fourcc, FPS, DESIRED_SIZE)
    
    for file_name in glob.iglob('*.jpg'):
        img = cv2.imread(file_name)
    
        # Resize image to fit into DESIRED_SIZE
        height, width, _ = img.shape
        proportion = min(DESIRED_SIZE[0]/width, DESIRED_SIZE[1]/height)
        new_size = (int(width*proportion), int(height*proportion))
        img = cv2.resize(img, new_size)
    
        # Centralize image in a black frame with DESIRED_SIZE
        target_size_img = np.zeros((DESIRED_SIZE[1], DESIRED_SIZE[0], 3), dtype='uint8') 
        width_offset = (DESIRED_SIZE[0] - new_size[0]) // 2
        height_offset = (DESIRED_SIZE[1] - new_size[1]) // 2
        target_size_img[height_offset:height_offset+new_size[1], 
                        width_offset:width_offset+new_size[0]] = img
    
        
        for _ in range(SLIDE_TIME * FPS):
            writer.write(target_size_img)
    
    writer.release()
    
    0 讨论(0)
  • 2020-12-29 10:30

    here's a cut-down version of a script I have that took frames from one video and them modified them(that code taken out), and written to another video. maybe it'll help.

    import cv2
    
    fourcc = cv2.cv.CV_FOURCC(*'XVID')
    out = cv2.VideoWriter('out_video.avi', fourcc, 24, (704, 240))
    
    c = cv2.VideoCapture('in_video.avi')
    
    while(1):
      _, f = c.read()
      if f is None:
        break
    
      f2 = f.copy() #make copy of the frame
      #do a bunch of stuff (missing)
    
      out.write(f2)  #write frame to the output video
    
    out.release()
    cv2.destroyAllWindows()
    c.release()
    

    If you have a bunch of images, load them in a loop and just write one image after another to your vid.

    0 讨论(0)
  • 2020-12-29 10:32

    You could use the Python interface of OpenCV, in particular a VideoWriter could probably do the job. From what I understand of the doc, the following would do what you want:

    w = cvCreateVideoWriter(filename, -1, <your framerate>, 
                            <your frame size>, is_color=1)
    

    and, in a loop, for each file:

    cvWriteFrame(w, frame)
    

    Note that I have not tried this code, but I think that I got the idea right. Please tell me if it works.

    0 讨论(0)
  • 2020-12-29 10:42

    Is it actually important to you that the solution should use python and produce a movie file? Or are these just your expectations of what a solution would look like?

    If you just want to be able to play back a bunch of jpeg files as a movie, you can do it without using python or cluttering up your computer with .avi/.mov/mp4 files by going to vidmyfigs.com and using your mouse to select image files from your hard drive. The "movie" plays back in your Web browser.

    0 讨论(0)
提交回复
热议问题