python图片保存为视频

# -*- coding: UTF-8 -*-
import os
import cv2
import time

# 图片合成视频
def picvideo(path):
    filelist = os.listdir(path) #获取该目录下的所有文件名

    writer = None
    for item in filelist:
        if item.endswith('.jpg'):   #判断图片后缀是否是.png
            item = path + '/' + item
            img = cv2.imread(item)  #使用opencv读取图像,直接返回numpy.ndarray 对象,通道顺序为BGR ,注意是BGR,通道值默认范围0-255。
            if writer is None:
                fourcc = cv2.VideoWriter_fourcc(*"MJPG")
                writer = cv2.VideoWriter("D:/dataset/lunch_scene_output.avi", fourcc, 24,
                                     (img.shape[1], img.shape[0]), True)
            if writer is not None:
                writer.write(img)


picvideo("D:/dataset/MVI_20011/")

你可能感兴趣的:(python图片保存为视频)