cv2将图片拼接为视频

示例代码:

import cv2
import os


def getName(x):
    if x <10:
        strRes = '0000' + str(x)
    elif x <100:
        strRes = '000' + str(x)
    elif x <1000:
        strRes = '00' + str(x)
    elif x < 10000:
        strRes = '0' + str(x)
    else:
        strRes = ''
    return strRes


# 图片路径
im_dir = 'pics'
# 输出视频路径
video_dir = 'pics/output.avi'
# 帧率
fps = 30
# 图片数
num = 772
# 图片尺寸
img_size = (1280, 720)

fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
video_writer = cv2.VideoWriter(video_dir, fourcc, fps, img_size)

for i in range(1, num):
    im_name = os.path.join(im_dir, getName(i) + '.png')
    print(getName(i))
    frame = cv2.imread(im_name)
    video_writer.write(frame)
    cv2.imshow('rr', frame)
    cv2.waitKey(20)


video_writer.release()
print('finish')

你可能感兴趣的:(Opencv,OpenCV-Python)