OpenCV 图片合成视频

# -*- coding: UTF-8 -*-

import os
import cv2
import time
import numpy as np
# 图片合成视频
def picvideo(path, path1, size, name=r"MOT"):
    # path = r'C:\Users\Administrator\Desktop\1\huaixiao\\'#文件路径
    filelist = os.listdir(path)  # 获取该目录下的所有文件名
    filelist.sort(key=lambda x: int(x[:-4]))
    filelist1 = os.listdir(path1)
    filelist1.sort(key=lambda x: int(x[:-4]))

    '''
    fps:
    帧率:1秒钟有n张图片写进去[控制一张图片停留5秒钟,那就是帧率为1,重复播放这张图片5次] 
    如果文件夹下有50张 534*300的图片,这里设置1秒钟播放5张,那么这个视频的时长就是10秒
    '''
    fps = 30
    # size = (1920, 1080)  # 图片的分辨率片
    size = (1920, 1080 * 2)
    file_path = r"OUTPUT/" + name + ".mp4"  # 导出路径
    fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X')  # 不同视频编码对应不同视频格式(例:'I','4','2','0' 对应avi格式)

    video = cv2.VideoWriter(file_path, fourcc, fps, size)

    for j, item in enumerate(filelist):
        font = cv2.FONT_HERSHEY_COMPLEX
        text = item[:-4]
        # 字体标注的位置, 内容,字体设置

        if item.endswith('.jpg'):  # 判断图片后缀是否是.png
            item = path + '/' + item
            item1 = path1 + "/" + filelist1[j]
            img = cv2.imread(item)  # 使用opencv读取图像,直接返回numpy.ndarray 对象,通道顺序为BGR ,注意是BGR,通道值默认范围0-255。
            img1 = cv2.imread(item1)  # 使用opencv读取图像,直接返回numpy.ndarray 对象,通道顺序为BGR ,注意是BGR,通道值默认范围0-255。
            cv2.putText(img, text, (100, 100), font, 2, (0, 0, 0), 1)
            cv2.putText(img1, filelist1[j][:-4], (100, 980), font, 2, (255, 255, 255), 1)
            res = np.vstack((img, img1))
            print("Processing : {} %".format((str(round(j / len(filelist), 3) * 100))[:4]))
            video.write(res)  # 把图片写进视频

    video.release()  # 释放


if __name__ == '__main__':
    # picvideo(path=r'MOT15', path1=r"../examples/", size=(1920, 1080), name="MOT15")
    # picvideo(path=r'MOT16', path1=r"../examples/", size=(1920, 1080), name="MOT16")
    # picvideo(path=r'MOT17', path1=r"../examples/", size=(1920, 1080), name="MOT17")
    # picvideo(path=r'MOT20', path1=r"../examples/", size=(1920, 1080), name="MOT20")
    picvideo(path=r'KITTI15', path1=r"../examples/", size=(1920, 1080), name="KITTI15")
    # picvideo(path=r'CityScapes', path1=r"../examples/", size=(1920, 1080), name="CityScapes")
    # picvideo(path=r'Sintel', path1=r"../examples/", size=(1920, 1080), name="Sintel")


你可能感兴趣的:(深度学习,图像处理,opencv)