python-----用多张图片生成视频

代码如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/19 16:42
# @Author  : xiaodai
# -*- coding: UTF-8 -*-
import cv2
import os

fps = 10
size = (1280, 720)
videowriter = cv2.VideoWriter("test.mp4", cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)
path = r'F:\temp\JPEGImages/'
for i in os.listdir(path):
    img = cv2.imread(path + i)
    videowriter.write(img)

如果图片较多,想要生成10秒一个视频,代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/19 16:42
# @Author  : xiaodai
# -*- coding: UTF-8 -*-

import cv2
import os

fps = 10
size = (1280, 720)
name = 1
videowriter = cv2.VideoWriter(str(name) + ".mp4", cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)
path = r'F:\temp\JPEGImages/'
num = 0
for i in os.listdir(path):
    img = cv2.imread(path + i)
    videowriter.write(img)
    if num % 100 == 0:
        videowriter = cv2.VideoWriter(str(name) + ".mp4", cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)
        name += 1
    num += 1

 

posted @ 2019-02-20 14:43  小呆丶  阅读(8799)  评论(1编辑  收藏  举报