#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/7/2 13:32 # @Author : xiaodai # -*- coding: UTF-8 -*- import os import cv2 from tqdm import tqdm from multiprocessing import Pool # 图片合成视频 def pic_to_video(path,save_path,time_dir): filelist = os.listdir(path) #获取该目录下的所有文件名 writer = None print('-------->',path,save_path) for item in tqdm(filelist): if item.endswith('.jpg'): #判断图片后缀是否是.jpg 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(save_path+'/'+time_dir+'.mp4', fourcc, 16, (img.shape[1], img.shape[0]), True) if writer is not None: writer.write(img) if __name__ == '__main__': org_path = r"F:\temp\test" for cam_name in os.listdir(org_path): cam_path = os.path.join(org_path,cam_name) p = Pool(8) for time_dir in os.listdir(cam_path): path = os.path.join(cam_path,time_dir,'JPEGImages') save_path = os.path.join(cam_path,time_dir,'JPEGImages') os.makedirs(save_path,exist_ok=True) p.apply_async(pic_to_video, args=(path, save_path,time_dir,)) p.close() p.join()
原文:https://blog.csdn.net/Hss891213/article/details/82468268