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.

Ive Zenzerovic
  • 73
  • 1
  • 11
  • 2
    Try `img = cv2.imread('C:\\Users\\Ive\\Downloads\\7.jpg',0)` (Escape backslash) See also [here](http://stackoverflow.com/a/31342428/5008845) – Miki Aug 09 '15 at 10:54
  • curious: Doesn't the python interface require using the `namedWindow` function first to create a window? – Photon Aug 09 '15 at 10:55
  • It's working, thanks. But can you explain why is it working? – Ive Zenzerovic Aug 09 '15 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. – Miki Aug 09 '15 at 10:58
  • ooooohhhhh okay. Thanks – Ive Zenzerovic Aug 09 '15 at 11:00
  • Photon, namedWindow is used when you want to create a window and assign image to it later. – Ive Zenzerovic Aug 09 '15 at 11:01

1 Answers1

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.

Community
  • 1
  • 1
Miki
  • 40,887
  • 13
  • 123
  • 202