问题描述
我有一个包含多个图像文件的文件夹。我将它们结合起来以15fps的速度编写视频。问题是由于某种原因,输出的最终视频的大小没有增加。 代码如下所示:
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'),15,size)
for filename in glob.glob('/mnt/d/projects/sample_vids/*.jpeg'):
print("h1")
for i in range(1440):
print(i)
img = cv2.imread(filename)
height,width,layers = img.shape
# size = (width,height)
# dim_width = int(img.shape[1] * scale_percent / 100)
# dim_height = int(img.shape[0] * scale_percent / 100)
# dim = (dim_width,dim_height)
resized = cv2.resize(img,(size[0] + i,size[1] + i),interpolation=cv2.INTER_AREA)
out.write(resized)
out.release()
在上面的代码中,将1440更改为120,结果与之前几乎相同。不确定,我在哪里弄错了
解决方法
-
- 使用
glob
收集所有图像
import cv2 import glob img_array = [] for i,filename in enumerate(glob.glob('/mnt/d/projects/sample_vids/*.jpeg')): img = cv2.imread(filename) height,width,layers = img.shape size = (width,height) resized = cv2.resize(img,(size[0] + i,size[1] + i),interpolation=cv2.INTER_AREA) img_array.append(resized)
- 使用
-
- 初始化。
VideoWriter
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'),15,size)
- 初始化。
-
- 创建视频
for i in range(len(img_array)): out.write(img_array[i]) out.release()