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

 

上一篇:Origin C创建视频


下一篇:将PHP最大执行时间设置为600是否安全?