0

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:\Users\Max\Documents\Pictures\pandas_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:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.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.

46
  • 2
    @MrIrrelevant try that, if it works then flag the question as a duplicate
    – Tino D
    Apr 3 at 8:44
  • 6
    the same question keeps getting asked every day. if anyone cares to flag as duplicate, I'd be happy. -- image is probably corrupted or in an unsupported format. until you present this file to us for inspection, we can't help. -- same goes for the path. we can't be sure that you're truthful about the existence of this file, or that your path is literally what you present. be maximally truthful. do not "change" anything. Apr 3 at 12:05
  • 1
    have you tried with a different jpg image ??
    – pippo1980
    Apr 3 at 19:53
  • 1
    I would still like feedback on my suggestion to test with os.path.isfile() Apr 18 at 11:09
  • 1
    os.path.isfile(path) returns false when I try printing it. Apr 18 at 11:13

2 Answers 2

0

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:\Users\Max\Documents\Pictures\pandas_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.

4
  • what is the variable image? it has never assigned any value in your code, can you explain further pls? Apr 18 at 7:42
  • @Mr.Irrelevant, sorry, I missed a line. The code should now be fine.
    – anmabe
    Apr 18 at 8:11
  • @anmabe no it's not "taking \ as escape characters". please learn about python's "r-string" feature. Apr 18 at 11:06
  • 1
    I know what r'' does: avoid taking \ as escape characters!!! I was just pointing out that in my computer using \ for the directory yielded the same error as the OP reported having. Then, I realised that my error arose because directories in my OS are written with / not with \, so it could not have been the same problem the OP had. Either way, @ChristophRackwitz, you should remember we are all here trying to help the OP, try to reproduce errors, and make errors ourselves along the way! So, you could spend more time collaborating with others, instead of arguing with them in the comment section.
    – anmabe
    Apr 18 at 11:17
-1
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

1
  • 2
    what for? this just handles whatever happened with the imread.
    – Tino D
    Apr 3 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.