python 多张图片合成视频

多张图片合成视频

# -*- coding: utf-8 -*-
import os
import cv2
from PIL import Image

def images_to_video(image_path, save_path):
    image_names = os.listdir(image_path)
    # 按照图片名称进行排序
    image_names.sort(key=lambda n: int(n[:-4]))
    # 设置写入格式
    fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
    # 设置的帧数
    fps = 4  
    image = Image.open(image_path + image_names[0])
    save_writer = cv2.VideoWriter(save_path, fourcc, fps, image.size)
    for image_name in image_names:
        im = cv2.imread(os.path.join(image_path, image_name))
        save_writer.write(im)
        print(image_name, '合并完成!')
    save_writer.release()
    print('视频写入完成!')

image_path =  'PATH/TO/IMAGE_DIRECTORY'
save_path = 'PATH/TO/MP4_FILE'
images_to_video(image_path, save_path)

你可能感兴趣的:(python,开发语言,计算机视觉)