我一直试图从所有文件夹中的静止图像创建视频。写入的文件大小为0KB。我已经检查过,并且glob.glob部分正确检索了所有文件(这就是注释的print(filename)行所针对的内容)。我尝试了多个fourcc选项,但没有一个起作用。有人看到会导致这种情况的问题吗?这也正在Jupyter Notebook的python 3上运行。

fold_file = fold +'/*jpg' #fold is just the path to folder containing the images
img_array=[]
for filename in glob.glob(fold_file):
    #print(filename)
    img=cv2.imread(filename)
    height, width, layer = img.shape
    size = (width,height)
    img_array.append(img)

out = cv2.VideoWriter('pleasework.avi',cv2.VideoWriter.fourcc('X','V','I','D') ,15,size)

for image in range(len(img_array)):
    out.write(image)


cv2.destroyAllWindows()
out.release()

最佳答案

这行代码可能是您遇到的问题:

for image in range(len(img_array)):
    out.write(image)
len()函数对序列中的项目数进行计数。为了便于讨论,我们假设img_array中有五个图像。然后len()将返回5。然后,我们将该长度值输入range()函数中,以生成从04的数字序列(即,最多5个但不包括5个数字)。

然后,我们使用for循环解析该范围,然后将04的数字放入out.write()方法中,而不是放入图像中。

您可能想要的是:
for image in img_array:
    out.write(image)
img_array是Python的list,因此可以由for循环本地解析,而不必使用任何长度计算等。

关于python - OpenCV不写文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61218661/

10-12 14:20
查看更多