Passing a Python Path object to OpenCV's imread
or imwrite
results in an undefined error:
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. The C++ functions expect string type filenames so that is what you have to provide to the Python functions as well.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.