在逐帧处理视频数据后,很多时候是将其保存为连续的图像,为了容易查看处理的结果要将连续的图形合成视频。本文提供此代码,且可以处理中文路径的图像数据,具体如下:
import os
import cv2
import glob
import matplotlib.pyplot as plt
def Img2Vi(dir_in):
height = 400 # 图像的高
width = 112 # 图像的宽
fps = 20 # 帧率
video = cv2.VideoWriter(dir_in+'结果.avi', cv2.VideoWriter_fourcc(*"I420"), fps, (width, height)) # 创建视频流对象-格式一
allfile = glob.glob(dir_in + '*.*')
for curfile in allfile:
ext = curfile.split('.')
ext = ext[-1]
if ext in ['jpg']: # ['jpg', 'png', 'bmp']
img = plt.imread(curfile) # 读取图像,此处使用plt即不受中英文路径的影响
video.write(img) # 向视频文件写入一帧--只有图像,没有声音
if __name__=='__main__':
dir_in = 'E:\\数据库\\01\\'
Img2Vi(dir_in)