且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OpenCV - 将图像保存到选择的特定文件夹

更新时间:2021-12-22 22:27:54

ebeneditos 提供的解决方案完美运行.

The solution provided by ebeneditos works perfectly.

但是如果你在一个大代码片段的几个部分有 cv2.imwrite() 并且你想更改保存图像的路径,你将不得不在每次出现时更改路径cv2.imwrite() 单独.

But if you have cv2.imwrite() in several sections of a large code snippet and you want to change the path where the images get saved, you will have to change the path at every occurrence of cv2.imwrite() individually.

正如 Soltius 所说,这是一个更好的方法.声明一个路径并将其作为字符串传递到 cv2.imwrite()

As Soltius stated, here is a better way. Declare a path and pass it as a string into cv2.imwrite()

import cv2
import os
img = cv2.imread('1.jpg', 1)
path = 'D:/OpenCV/Scripts/Images'
cv2.imwrite(os.path.join(path , 'waka.jpg'), img)
cv2.waitKey(0)

现在如果你想修改路径,你只需要改变path变量即可.

Now if you want to modify the path, you just have to change the path variable.

根据 Kallz 提供的解决方案编辑

Edited based on solution provided by Kallz