dq.shen
dq.shen

Reputation: 351

opencv-python cv2.imread() return a 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

Upvotes: 0

Views: 2526

Answers (1)

Anderson Lima
Anderson Lima

Reputation: 246

The filename param need to be a absolute/relative path, so, you need use /home/username/ instead ~.

Check imread documentation.

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)

Upvotes: 2

Related Questions