首页 > 解决方案 > cv2.imwrite 不保存任何图像

问题描述

我正在尝试运行一个简单的示例代码来在 python3 上使用 opencv 编写图像。代码参考:1

import cv2
import os

image_path = r'C:\Users\840g1 touch\Desktop\B2.jpg'

directory = r'C:\Users\840g1 touch\Desktop'

img = cv2.imread(image_path)

os.chdir(directory)

print("Before saving image:")
print(os.listdir(directory))

# Filename
filename = 'savedImage.jpg'

cv2.imwrite(filename, img)

print("After saving image:")
print(os.listdir(directory))

print('Successfully saved')

图像正在显示,除了图像之外的所有内容都没有保存在任何地方。我在 Windows 上使用 Anaconda。不确定问题是否与代码或我的电脑有关。

任何帮助深表感谢!

标签: pythonopencvopencv-python

解决方案


您没有提供路径,imwrite因此它会写入您的 python当前工作目录

换行:

cv2.imwrite(filename, img)

类似于:

cv2.imwrite(os.path.join(directory,filename), img)

注意:你可以得到你当前的工作目录

os.getcwd()

推荐阅读