Assertion failure : size.width>0 && size.height>0 in function imshow

73,483

Solution 1

The image fails to load (probably because you forgot the leading / in the path). imread then returns None. Passing None to imshow causes it to try to create a window of size 0x0, which fails.

The poor error handling in cv probably owes to its quite thin wrapper layer on the C++ implementation (where returning NULL on error is a common practice).

Solution 2

it's the path which is causing the problem, i had the same problem but when i gave the full path of the image it was working perfectly.

Solution 3

While using Raspbian in Rpi 3 I had the same problem when trying to read qrcodes. The error is because cv2 was not able to read the image. If using png image install pypng module.

sudo pip install pypng

Solution 4

Use r in the code where you specified the file address.
For Example:

import cv2
img = cv2.imread(r'D:\Study\Git\OpenCV\resources\lena.png')
cv2.imshow('output', img)
cv2.waitKey(0)

r stands for "raw" and will cause backslashes in the string to be interpreted as actual backslashes rather than special characters.

Solution 5

In my case, I had forgotten to change the working directory of my terminal to that of my code+testImage. Hence, it failed to find the image there.

Finally, this is what worked for me:

I saved the image and Python file on Desktop. I changed my cmd directory to it,

cd Desktop

And then checked for my file:

ls

And this was my code that worked:

import cv2
import numpy as np

im = cv2.imread('unnamed.jpg')
#Display the image
cv2.imshow('im',im)
cv2.waitKey(2000) #Milliseconds
Share:
73,483
Bibek Ghimire
Author by

Bibek Ghimire

BY DAY : I AM A TRAVELLER BY NIGHT: FUNNY CODER WRITING STUPID AND FUNNY CODE INSPIRED FROM DAILY LIFE

Updated on July 17, 2022

Comments

  • Bibek Ghimire
    Bibek Ghimire almost 2 years

    i am using opencv2 and python on raspberry pi. and i am new with python and opencv. i tried to read a jpeg image and display image it shows the following error:

    /home/pi/opencv-2.4.9/modules/highgui/src/window.cpp:269: \
      error: (-215) size.width>0 &&  size.height>0 in function imshow.
    

    and the code is:

    import cv2
    # windows to display image
    cv2.namedWindow("Image")
    # read image
    image = cv2.imread('home/pi/bibek/book/test_set/bbb.jpeg')
    # show image
    cv2.imshow("Image", image)
    # exit at closing of window
    cv2.waitKey(0)
    cv2.destroyAllWindows()