python - 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。不确定问题是否与代码或我的电脑有关。
任何帮助深表感谢!
解决方案
您没有提供路径,imwrite
因此它会写入您的 python当前工作目录。
换行:
cv2.imwrite(filename, img)
类似于:
cv2.imwrite(os.path.join(directory,filename), img)
注意:你可以得到你当前的工作目录
os.getcwd()
推荐阅读
- mysql - 如何获得父母类别的总数?
- java - Java 睡眠命令忽略睡眠前的命令
- c# - 在运行时缩放精灵后了解精灵的实际尺寸
- javascript - 当我在数组过滤器中运行 array.some 时会发生什么
- python-3.x - 如何从类中获取 href 值 - Python - Selenium
- jms - E 02/07/19-07:51:49.358 [-5] JMSListener 异常:java.lang.ClassCastException:com.ibm.jms.JMSMessage 无法转换为 javax.jms.TextMessage
- wordpress - 如何在页面管理员的 post_type 列表顶部添加 HTML?
- python - “NoneType”对象没有“附加”属性。如何在循环中将 int 附加到列表中?
- python - Django从外部api缓存图像
- ios - 如果iOS设备中不存在用于应用商店提交的另一个应用程序,如何继续调用另一个应用程序?