【问题标题】:cv2.imwrite not saving any imagecv2.imwrite 不保存任何图像
【发布时间】:2020-11-19 13:18:06
【问题描述】:

我正在尝试运行一个简单的示例代码来在 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。不知道问题是否与代码或我的电脑有关。

非常感谢任何帮助!

【问题讨论】:

  • 确保图片加载正确,即img not None

标签: python opencv opencv-python


【解决方案1】:

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

换行:

cv2.imwrite(filename, img)

类似于:

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

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

os.getcwd()

【讨论】:

  • 还是没有运气。我在不同的系统上尝试了相同的代码,它工作正常。
猜你喜欢
  • 2020-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
相关资源
最近更新 更多