os.path.isfile() returns False for a file that can be found with the explorer

Question:

Hi I am trying to read in and display a picture with cv2 for further analysis with the following code:

Edit: I added the path with my username being clear to prove it doesn’t contain wacky characters. I also tried with multiple .jpg files. I am using opencv-python version 4.9.0.80

import cv2 

path = r'C:UsersMaxDocumentsPicturespandas_secondary.jpg'

image = cv2.imread(path) 

window_name = 'image'

cv2.imshow(window_name, image) 

cv2.waitKey(0) 

cv2.destroyAllWindows() 

However, this gives me the following error:

error: OpenCV(4.9.0) D:aopencv-pythonopencv-pythonopencvmoduleshighguisrcwindow.cpp:971: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

The path to my image is correct in my first posted Question I only anonymized it for the question. Now I added the actual path. what am I doing wrong here?

Update: moving the picture to the same directory as my python code leads to the code running for ever, even if the picture is only 20 kb sized. Displaying the picture with pillow works fine though. I also reinstalled cv2 by:

pip install --upgrade --force-reinstall opencv-python

which didn’t solve the problem.

This is the picture:

Sample Picture

os.path.isfile(path)

returns false.

Asked By: Mr. Irrelevant

||

Answers:

if image is None:
    print("Error: Image not loaded. Check the file path.")
else:
    window_name = 'image'
    cv2.imshow(window_name, image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

add this and check

Answered By: Arjun Mani

The problem with this error (error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow') is that either the image does not exist, it is corrupted, or the path provided is wrong. Reproducing your problem in my computer, I believe your error lies in the path. Try substituting the forward-slashes for backward-slashes. That solved the problem for me.

If this is not your problem, as some users have pointed out in the comments, try the following code on your computer:

import cv2 

path = r'C:UsersMaxDocumentsPicturespandas_secondary.jpg'
window_name = 'image'
image = cv2.imread(path)

if image is None:
    raise Exception("Either the image does not exist, it is corrupted, or the path provided is wrong.")
else:
    cv2.imshow(window_name, image)
    cv2.waitKey(0)
    cv2.destroyAllWindows() 

If the exception is raised, you have a problem with the library and not with Python. I have tried the above on my computer and it works perfectly (once I changed the orientation of the slashes). If for you, it still does not, maybe try changing the absolute path to the image for a relative one. Finally, what you can do as a last resort is uninstall and reinstall OpenCV, but this is likely not the case. Use the below commands:

python3 -m pip uninstall opencv-python

and

python3 -m pip install opencv-python

EDIT: For some reason, if you are running your code in VS Code while using relative paths, OpenCV does not recognise the file (yielding the error you pointed out) unless you have it in your workspace within the editor.

Answered By: anmabe
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.