python图片生成视频代码

image_path为图片所在目录,save_path 是视频保存的路径, save_name 保存的视频名

import os
import cv2

def images_to_video(save_path ,video_folder, rep=5, result_filename=None):

    if result_filename is None:
        result_filename = "{}.avi".format(save_path)
    #video_folder:文件目录
    #os.listdir(video_folder):文件夹下边的图片列表
    #f:0.jpg.........jpg
    #splitext(f[0]):把数字提取出来
    images_name = {int(os.path.splitext(f)[0]): os.path.join(video_folder, f) for f in os.listdir(video_folder)}
    # read the first frame and find the height, width and layers of all the images
    img = cv2.imread( images_name[0] )
    height, width, layers = img.shape


    # initiate the video with width, height and fps = 25
    four_cc = cv2.VideoWriter_fourcc(*"XVID")  # avi
    # four_cc = cv2.VideoWriter_fourcc(*"mp4v")  # mp4
    video = cv2.VideoWriter(result_filename, four_cc, 25, (width, height))

    for i in range(0, len(images_name)):
        for j in range(rep):
            img = cv2.imread(images_name[i])
            video.write(img)

        # print the progress bar
        if i % 100 == 0:
            print("Done {}%".format((i*100)/len(images_name)))

    cv2.destroyAllWindows()
    video.release()
    print("Done!")

    return None

if __name__ == '__main__':
    image_path = ""
    save_path = ""
    save_name = ""
    images_to_video(os.path.join(save_path,save_name), images_path, 1)
    #1S播放1次图片
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值