Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief video helpers 

4""" 

5import os 

6 

7 

8def make_video(images, outvid=None, fps=5, size=None, 

9 is_color=True, format="XVID"): 

10 """ 

11 Creates a video from a list of images. 

12 

13 @param outvid output video 

14 @param images list of images to use in the video 

15 @param fps frames per second 

16 @param size size of each frame 

17 @param is_color color 

18 @param format see `fourcc <http://www.fourcc.org/codecs.php>`_ 

19 @return `VideoWriter <http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/ 

20 py_gui/py_video_display/py_video_display.html>`_ 

21 

22 The function relies on `opencv <http://opencv-python-tutroals.readthedocs.org/en/latest/>`_. 

23 By default, the video will have the size of the first image. 

24 It will resize every image to this size before adding them to the video. 

25 The function does not use :epkg:`moviepy` but it is a 

26 a recommended module to do that. 

27 """ 

28 if len(images) == 0: 

29 raise ValueError("no image to convert into a video") 

30 from cv2 import VideoWriter, VideoWriter_fourcc, imread, resize # pylint: disable=E0401 

31 fourcc = VideoWriter_fourcc(*format) 

32 vid = None 

33 for image in images: 

34 if not os.path.exists(image): 

35 raise FileNotFoundError(image) 

36 img = imread(image) 

37 if vid is None: 

38 if size is None: 

39 size = img.shape[1], img.shape[0] 

40 vid = VideoWriter(outvid, fourcc, float(fps), size, is_color) 

41 if size[0] != img.shape[1] and size[1] != img.shape[0]: 

42 img = resize(img, size) 

43 vid.write(img) 

44 vid.release() 

45 return vid