【问题标题】:OpenCV Not Writing FileOpenCV 不写入文件
【发布时间】:2020-04-15 07:33:50
【问题描述】:

我一直在尝试从文件夹中的所有静止图像创建视频。写入的文件大小为 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()

【问题讨论】:

标签: python opencv


【解决方案1】:

这行代码可能是你的问题:

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

len() 函数计算序列中的项目数。让我们假设您在img_array 中有五张图片。然后len() 将返回5。然后我们将该长度值输入到range() 函数中,以生成从04 的数字序列(即最多但不包括5)。

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

你可能想要的是这个:

for image in img_array:
    out.write(image)

img_array 是一个 Python list,因此可以被 for 循环解析,而无需使用任何类型的长度计算等。

【讨论】:

  • 谢谢!这确实使文件实际上具有大小。但是,我不断收到一条错误消息,提示“选择其他内容进行播放。此项目的格式我们不支持。”在尝试播放视频时。对这种情况有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 2015-03-10
  • 2019-07-11
  • 2020-06-04
  • 2017-12-16
  • 2015-08-05
  • 2017-06-17
  • 2013-04-25
  • 2020-12-15
相关资源
最近更新 更多