为什么cv2.imwrite()不将文件保存在目录中?

时间:2019-10-14 08:52:36

标签: python opencv

The image is not saving in the location using cv2.imwrite()

import cv2
import os
path=r'C_\Users\Romeo\Desktop\images'
cam = cv2.VideoCapture(1)
ret,image=cam.read()
cv2.imshow("bnbnhj",image)
cv2.waitKey(0)
cam.release()
image_name='10.jpg'
cv2.imwrite(os.path.join(path,image_name),image)
cv2.waitKey(0)
cv2.destroyWindows()

图像显示在新选项卡中,但是图片未保存在文件目录中

1 个答案:

答案 0 :(得分:0)

您的目录可能不存在。不幸的是,imwrite在这种情况下不会引发任何错误。

import cv2
import os

path = r'C:\Users\Romeo\Desktop\images'
cam = cv2.VideoCapture(1)
ret, image = cam.read()
cv2.imshow("bnbnhj", image)
cv2.waitKey(0)
cam.release()
image_name = '10.jpg'
if not os.path.isdir(path):
    print("No such a directory: {}".format(path))
    exit(1)

cv2.imwrite(os.path.join(path, image_name), image)
cv2.waitKey(0)
cv2.destroyAllWindows()