cv2.imwrite 返回 false 而不是保存图像

cv2.imwrite returning false instead of saving image

我正在编写保存图像的代码。而不是保存图像 cv2.imwrite 函数返回 false。我在下面的代码中重现了这个问题:

Uni_ID = 123
cam=cv2.VideoCapture(0)
rett, img = cam.read()

now = datetime.now()
dtString =  now.strftime('%d-%m-%Y')
timeString = now.strftime('%H:%M:%S')
path = 'IMG'
name = path + '/' + dtString
#     print(name)
if not os.path.exists(name):
    os.makedirs(name)
imgName = name + '/'+ str(Uni_ID) + '_' + timeString + '.jpg'
print(imgName)
cv2.imwrite(imgName, img)

最后一行的 cv2.imwrite 函数没有保存图像,而是返回 false。代码的输出是:

IMG/03-09-2020/123_17:59:58.jpg
False

您不能在文件名中使用 :

>>> cv2.imwrite(r'123_17:59:58.jpg', np.array(i))
False
>>> cv2.imwrite(r'123_17_59_58.jpg', np.array(i))
True

如果您不确定文件名是否有效 - 先尝试打开它。

>>> f = open(r'123_17:59:58.jpg', 'wt')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
OSError: [Errno 22] Invalid argument: '123_17:59:58.jpg'

>>> f = open(r'123_17_59_58.jpg', 'wt')
>>>