Convert Multiple Frames in a Specified Folder to Video

30 篇文章 1 订阅
19 篇文章 3 订阅
该博客介绍如何利用OpenCV从一系列PNG图片创建视频。首先通过glob获取指定目录下的所有PNG文件,然后按照文件名中的数字进行排序。接着,定义帧列表并将图片读入内存。最后,设置输出视频的名称和参数,使用VideoWriter将图片写入视频流,并释放资源。此过程适用于处理以特定格式命名的图像序列。
摘要由CSDN通过智能技术生成
import glob
import cv2
import os

files = glob.glob('bamboo_2/floviz/png/*.png')
print(files)
files.sort(key=lambda x: x[15:-4])
files.sort()
print(files)
frame_list = []

for i in files:
    img = cv2.imread(i)
    frame_list.append(img)

size = (frame_list[1].shape[1], frame_list[1].shape[0])

video_name = os.path.join(files[0].split('.')[0][:-10], 'flow.avi')
print(video_name)
out = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc(*'DIVX'), 25, size)

for i in range(len(frame_list)):
    out.write(frame_list[i])

out.release()

"glob" is used to get all the png files in the specified directory. If your files are named with suffix like ‘jpg, ppm, pgm’, you can also change png to crospending format.
files.sort(key=lambda x: x[15:-4]) files.sort() is used to sort all the files you’ve got by glob. Cause files obtained by glob may not be in the right order. x[15:-4] should be modified by your case. In my case, the obtained files’ name are in the format as “bamboo_2/floviz/png/frame_0009.flo.png”, ‘15’ means x starts at ‘0’, and ‘-4’ means x ends at ‘.’. Thus files will be sort by the numbers in the file name.
os.path.join(files[0].split('.')[0][:-10], 'flow.avi') specifies the path and name of output video.
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值