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.