-3

I am trying to start with basics of opencv with python but when i executed the below code :

import cv2
import numpy as np

img = cv2.imread('bg.jpg',cv2.IMREAD_GRAYSCALE)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

But i am getting this error:-

Traceback (most recent call last):
File "F:\OpenCV Programs\OpenCV1.py", line 7, in <module>
   cv2.imshow('image',img)
error: C:\build\master_winpack-bindings-win32-vc14-
static\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 
&& size.height>0 in function cv::imshow

Plz help me out thank you..!

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Why is this tagged both python-3.x and python-2.7? Do you need code that runs in both? – abarnert Mar 08 '18 at 18:30
  • i m trying on both cause its giving this error..do you have any solution?? – Tejas Shaha Mar 08 '18 at 18:31
  • 2
    why not simple search the web for the error message. you don't have to post a question on StackOverflow for that... – Piglet Mar 08 '18 at 18:37
  • You may want to read [this Q&A](https://stackoverflow.com/questions/23628325/cv2-imread-checking-if-image-is-being-read) as well. Things sometimes fail in the real world, you have to write your software to account for that possibility. – Dan Mašek Mar 08 '18 at 18:40

2 Answers2

1

The code you posted works, if there is an image called bg.jpg within the same directory where you are running the code.

Anyway, you could try to reference the full path to the image:

import os

img_path = os.path.join(os.getcwd(), 'bg.jpg')
img = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
drec4s
  • 7,946
  • 8
  • 33
  • 54
0

The image you attempt to load does not exist.

As you cannot display a non-existing image you get an error messsage for trying.

You'll most likely messed up the path. Maybe use an absolute path to the image.

Piglet
  • 27,501
  • 3
  • 20
  • 43