Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python JPEG to movie

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 multiple JPEGs from that directory and "paste" them into a grid which is one frame of a movie file. Which modules could achieve this?

like image 946
digit Avatar asked Mar 20 '12 21:03

digit


3 Answers

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.

like image 96
agravier Avatar answered Nov 12 '22 03:11

agravier


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.

like image 29
user1269942 Avatar answered Nov 12 '22 02:11

user1269942


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()
like image 1
wviana Avatar answered Nov 12 '22 02:11

wviana