【发布时间】:2017-01-08 15:35:46
【问题描述】:
我编写了一个脚本,它使用 cv2 和其他一些模块将视频文件分割成帧。到目前为止,我很高兴粘贴文件路径并运行代码,但现在我希望用户输入文件路径和名称以响应提示。这应该很容易,但我在让 os.path 为我工作时遇到了很多麻烦。主要问题是我希望每个图像文件(即帧)的名称中都有一个数字,以显示它在序列中的位置。下面的代码是我所拥有的:
filepath = input('Please enter the filepath for the clip: ')
clip = cv2.VideoCapture(filepath)
#### This code splits the clip into scenes
filepath1 = input('Please enter the filepath for where the frames should be saved: ')
name = input('Please enter the name of the clip: ')
ret, frame = clip.read()
count = 0
ret == True
while ret:
ret, frame = clip.read()
cv2.imwrite((os.path.join(filepath1,name, '(%d)','.png') % count, frame))
count += 1
但是,会产生以下错误:
cv2.imwrite((os.path.join(filepath1,name, '(%d)','.png') % count, frame))
TypeError: Required argument 'img' (pos 2) not found
在 os.path.join 命令的括号中包含 % count, frame 变量会产生不同的错误:
TypeError: not all arguments converted during string formatting
它应该做的是将一些名为name(x)的.png文件写入MYcomputer/mydesktop/myfolder/这样的位置。我不确定这里出了什么问题——感谢任何帮助!
【问题讨论】:
标签: python filepath os.path cv2