# coding=utf-8
import os
import cv2
from PILimport Image
def makevideo(path, fps):
""" 将图片合成视频. path: 视频路径,fps: 帧率"""
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
path1 ='C:/Users/Administrator/Desktop/zhongxing/trackdata/A-data/Track10' #图片文件夹
im = Image.open('C:/Users/Administrator/Desktop/zhongxing/trackdata/A-data/Track10/1.jpg')#第一张图片
print(im.size)
vw = cv2.VideoWriter(path, fourcc, fps, im.size)
filenames = os.listdir(path1)
#key=lambda x: int(x[:-4])
#filenames = sorted(filenames) a= int(float(value))
filenames.sort(key=lambda x:int(float(x.split('.')[0])))#按照图片名字顺序排序 必须是数字不能有字符串
for iin filenames:
frame = cv2.imread(path1 +'/'+ i)
vw.write(frame)
if __name__ =='__main__':
video_path ='C:/Users/Administrator/Desktop/zhongxing/trackdata/A-data/test_new10.mp4' #生成的视频想放置位置
makevideo(video_path, 10)# 图片转视频
网友评论