opencv-python cv2.imread() 返回一个 NoneType [英] opencv-python cv2.imread() return a NoneType
本文介绍了opencv-python cv2.imread() 返回一个 NoneType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用 cv2.imread('~/Download/image.jpg')
读取图像,但它总是返回 NoneType
.看来这个函数不能读取任何图像.我很确定这条路是正确的.有人知道吗?谢谢
I try to use cv2.imread('~/Download/image.jpg')
to read an image, but it always returned a NoneType
. It seems that this function can not read any image. I am pretty sure that the path is right. Does anyone know something?
Thanks
推荐答案
filename 参数需要是绝对/相对路径,所以,你需要使用 /home/username/ 代替~.
检查 imread 文档.
如果需要使用~,可以试试expanduser:
If you need to use ~, you can try expanduser:
from os.path import expanduser
filename = expanduser("~") + '/Download/image.jpg'
img = cv2.imread(filename)
Python3.6
from pathlib import Path
filename = f'{str(Path.home())}/Download/image.jpg'
img = cv2.imread(filename)
这篇关于opencv-python cv2.imread() 返回一个 NoneType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文