height=mat.shape[0] AttributeError: 'NoneType' object has no attribute 'shape'

I was recently doing proportion of total pixels pixels of a statistical picture of a certain color, you need to read the folder some pictures, but when using cv2.imread, reported this error:
AttributeError: ' NoneType 'object has no attribute' shape '
I found some solutions will say this is the path did not write right, I can see their own path in the code, and then into the path of discovery is not an absolute right, the following code, then I wanted a print out what value item in the for loop, find the file name can be printed out, but no picture is found. Later, it must be the wrong path in a for loop, add the following code on it.

picture_path="/home/wsb/桌面/picture"
picture_list=os.listdir(picture_path)
total_picture=len(picture_list)
per=[]
for item in picture_list:
    mat=cv2.imread(item)
    height=mat.shape[0]
    width=mat.shape[1]

Modify the code thick

picture_path="/home/wsb/桌面/picture"
picture_list=os.listdir(picture_path)
total_picture=len(picture_list)
per=[]
for item in picture_list:
    src = os.path.join(os.path.abspath(picture_path), item)
    mat=cv2.imread(item)
    height=mat.shape[0]
    width=mat.shape[1]

Successfully resolved the problem.

Guess you like

Origin blog.csdn.net/weixin_42535742/article/details/90763216