img2avi

import cv2
import os
import os.path as osp


def img2video(img_dir, img_size, video_dir, fps):
    fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')  # opencv3.0
    videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)

    for idx in sorted(os.listdir(img_dir)):
        img = osp.join(img_dir, idx)
        frame = cv2.imread(img)
        # print(frame.shape)  # h, w, c (480, 640, 3)
        videoWriter.write(frame)

    videoWriter.release()
    print('Finish changing!')


if __name__ == '__main__':
    img_dir = 'your img path'
    par_dir = osp.dirname(img_dir)
    video_path = osp.join(par_dir, 'output.avi')

    fps = 20
    img_size = (1280, 720)  # w, h 根据自己具体的图片大小进行设置
    img2video(img_dir=img_dir, img_size=img_size, video_dir=video_path, fps=fps)

你可能感兴趣的:(python)