将逐帧图片生成mp4格式视频——pyhton实现

import cv2
img = cv2.imread('/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/s0.png')
imgInfo = img.shape
size = (imgInfo[1],imgInfo[0])
print(size)
# 参数:1 file name 2 编码器 3 帧率 4 size
videoWrite = cv2.VideoWriter('mot.mp4',cv2.VideoWriter_fourcc(*'mp4v'),10,size)# 写入对象

# 0-197张图片
for i in range(0,198):
    fileName = '/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/s'+str(i)+'.png'
    img = cv2.imread(fileName)
    videoWrite.write(img) # 将图片写入视频
videoWrite.release()
print('end!')


# import cv2
# import glob
# img_array = []
# for filename in glob.glob('/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/*.png'):
#     # 需要将路径更改为自己存储图片的路径
#     img = cv2.imread(filename)
#     height, width, layers = img.shape
#     size = (width, height)
#     img_array.append(img)
# out = cv2.VideoWriter('mot.avi', cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
# for i in range(len(img_array)):
#     out.write(img_array[i])
# out.release()

********************************************文件名补0操作***********************************************

import cv2
# img = cv2.imread('/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/s0.png')
img = cv2.imread('F:/MOT/3D-Multi-Object-Tracker/evaluation/data/gt/davis/davis_unsupervised_val/bike-packing/00000.png')
imgInfo = img.shape
size = (imgInfo[1],imgInfo[0])
print(size)
# 参数:1 file name 2 编码器 3 帧率 4 size
videoWrite = cv2.VideoWriter('mot.mp4',cv2.VideoWriter_fourcc(*'mp4v'),10,size)# 写入对象

# 1-198张图片
for i in range(0,69):
    num = "%05d" % i    # 补0操作(5位数补0)
    # fileName = '/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/s'+str(num)+'.png'
    fileName = 'F:/MOT/3D-Multi-Object-Tracker/evaluation/data/gt/davis/davis_unsupervised_val/bike-packing/' + str(num) + '.png'
    img = cv2.imread(fileName)
    videoWrite.write(img) # 将图片写入视频
videoWrite.release()
print('end!')


# import cv2
# import glob
# img_array = []
# for filename in glob.glob('/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/*.png'):
#     # 需要将路径更改为自己存储图片的路径
#     img = cv2.imread(filename)
#     height, width, layers = img.shape
#     size = (width, height)
#     img_array.append(img)
# out = cv2.VideoWriter('mot.avi', cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
# for i in range(len(img_array)):
#     out.write(img_array[i])
# out.release()

你可能感兴趣的:(python,plt,python,opencv)