1

I am currently following a tutorial on how to use OpenCV with python but something isn't working. When i run this code that should display an image i get this error: error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\highgui\src\window.cpp:271: error: (-215) size.width>0 && size.height>0 in function cv::imshow

Here is the code

import numpy as np
import cv2

img = cv2.imread('C:\Users\Ive\Downloads\7.jpg',0)
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Someone already asked this question on here but the answer didn't work for me. The answer was to remove the unnecessary quotes. But i don't have them.

6
  • 2
    Try img = cv2.imread('C:\\Users\\Ive\\Downloads\\7.jpg',0) (Escape backslash) See also here Commented Aug 9, 2015 at 10:54
  • curious: Doesn't the python interface require using the namedWindow function first to create a window? Commented Aug 9, 2015 at 10:55
  • It's working, thanks. But can you explain why is it working? Commented Aug 9, 2015 at 10:55
  • As you know, "\n" (new line), "\t" (tab) etc have a well defined meaning. "\U", "\I" etc don't. So your string is ill-formed. Try printing the string with and without escaping backslashes, and you'll see that one is a valid pathname, the other is not. Commented Aug 9, 2015 at 10:58
  • ooooohhhhh okay. Thanks Commented Aug 9, 2015 at 11:00

1 Answer 1

3

You need to escape backslashes, or your string will be ill-formed. This will work:

img = cv2.imread('C:\\Users\\Ive\\Downloads\\7.jpg',0)

You can find here additional information.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.