简体   繁体   English

将 OpenCV imread 和 imwrite 与 Python Path 对象一起使用会给出 SystemError:<built-in function imread> 返回 NULL 没有设置错误</built-in>

[英]Using OpenCV imread and imwrite with Python Path objects gives SystemError: <built-in function imread> returned NULL without setting an error

Passing a Python Path object to OpenCV's imread or imwrite results in an undefined error:将 Python 路径 object 传递给 OpenCV 的imreadimwrite会导致未定义的错误:

from pathlib import Path
import cv2

img_path = Path("test.png")
img = cv2.imread(img_path)

Results in:结果是:

Traceback (most recent call last):
  File ".\secondary_image_generation.py", line 36, in <module>
    img = cv2.imread(img_path)
SystemError: <built-in function imread> returned NULL without setting an error

Why is this and how can I avoid it?为什么会这样,我该如何避免?

OpenCV library sources are written in C++ and the Python bindings are primarily auto-generated and don't do much more than wrap the C++ functions. OpenCV 库源代码是在 C++ 中编写的,而 Python 绑定主要是自动生成的,除了包装 C++ 函数外没有做更多的事情。 The C++ functions expect string type filenames so that is what you have to provide to the Python functions as well. C++ 函数需要字符串类型的文件名,因此这也是您必须提供给 Python 函数的内容。

Doing the following resolves the problem by resolving the full path and converting it to a string:执行以下操作可通过解析完整路径并将其转换为字符串来解决问题:

img_path = Path("test.png")
img = cv2.imread(str(img_path.resolve()))

This is still an open feature request for the current version of OpenCV.这仍然是当前版本 OpenCV 的开放功能请求

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cv2.imread 错误:img = cv2.imread(0) 系统错误:<built-in function imread> 返回 NULL 没有设置错误</built-in> - cv2.imread error:img = cv2.imread(0) SystemError: <built-in function imread> returned NULL without setting an error 系统错误:<built-in function imread> 返回 NULL 没有设置错误(tkinter)</built-in> - SystemError: <built-in function imread> returned NULL without setting an error (tkinter) <built-in function imread>返回 NULL 没有设置错误</built-in> - <built-in function imread> returned NULL without setting an error cv2.imwrite() 系统错误:<built-in function imwrite> 返回 NULL 没有设置错误</built-in> - cv2.imwrite() SystemError: <built-in function imwrite> returned NULL without setting an error 使用 cv2.imread:“<built-in function imread> 没有设置错误就返回NULL”,好像无法打开图片或获取数据</built-in> - Using cv2.imread: “<built-in function imread> returned NULL without setting an error”, as if it can't open the picture or get the data python opencv '系统错误:<built-in function drawkeypoints> 在没有设置错误的情况下返回 NULL'</built-in> - python opencv 'SystemError: <built-in function drawKeypoints> returned NULL without setting an error' 对象检测图像文件夹读取返回<built-in function imread>返回 NULL 而不设置错误 - Object Detection Images folder read return <built-in function imread> returned NULL without setting an error 系统错误:<built-in function puttext> 返回 NULL 没有设置错误</built-in> - SystemError: <built-in function putText> returned NULL without setting an error 如何修复“系统错误:<built-in function 'name'> 在 Python C 扩展中返回 NULL 而不设置错误” - How to fix "SystemError: <built-in function 'name'> returned NULL without setting an error" in Python C Extension <built-in function imshow>返回 NULL 而不设置错误 - <built-in function imshow> returned NULL without setting an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM