5

I am running some basic OpenCV code l, but it results in error. The code is

import numpy as np
import cv2
img=cv2.imread('C:\Users\Pravin\Desktop\a.jpeg',1)
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.Waitkey(10000)
cv2.imshow('cv2.WINDOW_NORMAL',img)
cv2.destoryAllWindows()

The error for cv2.imshow() is

Traceback (most recent call last):
 File "<pyshell#3>", line 1, in <module>
cv2.imshow('image',img)
error: ..\..\..\src\opencv\modules\highgui\src\window.cpp:261: error: (-215)
size.width>0 && size.height>

Why does this error happen, and how can I avoid it?

1
  • Maybe you would want to escape the back slashes. Commented Apr 3, 2014 at 6:22

5 Answers 5

6

Most likely, the imread call didn't succeed. Make sure the image "C:\Users\Pravin\Desktop\a.jpeg" exists. (The extension .jpeg seems unusual, maybe it has to be .jpg?)

Also, as Hyperboreus suggests, please, try using forward slashes in the filename "C:/Users/Pravin/Desktop/a.jpg", or escape backslashes

"C:\\Users\\Pravin\\Desktop\\a.jpg"
Sign up to request clarification or add additional context in comments.

3 Comments

"C:\Users\Pravin\Desktop\a.jpeg" surely doesn't exist, but due to another reason.
I've changed it to a.jpg but after changing it also it gives error
We can use file("the\file\path\passed\to\imread") to verify whether that image path exists or is correct.
3

The error says that the image you opened doesn't satisfy the condition height > 0 and width > 0. This may have several reasons.

Most of the times, it is due to an inexistent image address given in imread.

Sometimes it may be also because imread failed to load the image. For example, if you write some random strings in notepad and save the file as a.jpg, imread will likely not be able to load it.

Comments

1

Try this...

import numpy as np
import cv2
img = cv2.imread('E:/Images/ece/1.png',1)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

1 Comment

How does this solve the problem? Is it because you found the right file? If so, then it's redundant and a duplicate of Sergei Nosov's answer. Is it because of your slightly different initialization logic? Then that would be good to know.
1

For me it worked when i just changed jpeg to jpg

Try this, may be it will work

import numpy as np
import cv2
img=cv2.imread('C:\Users\Pravin\Desktop\a.jpg',1)    #changed image format to jpg
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.Waitkey(10000)
cv2.imshow('cv2.WINDOW_NORMAL',img)
cv2.destoryAllWindows()

Comments

0

It is because, python compiler cannot find the image in the place. if you copy the image in the python working directory and do this. it worked for me.

    # keep image in the current working directory
    img=cv2.imread('roi.jpg',1) 
    cv2.imshow('image',img)

1 Comment

How is this effectively any different from Sergei Nosov's answer which says to make sure the file location is correct?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.