Python3 - OpenCV & cv2.error: (-215) Unable to show captured image

J.Doe picture J.Doe · Aug 30, 2017 · Viewed 49.4k times · Source

I am trying to using opencv-3.3.0, cv2 & python3.5.

But, I can't seem to show image I have captured.

  • I read all the documentation.
  • I tried all possible answers.
  • But, I still unsuccessful.

What am I missing?

Code:

import numpy as np
import cv2

img=cv2.imread("F:/Train/sreen.png")
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The Error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file D:\Build\OpenCV\opencv-3.3.0\moules\highgui\src\window.cpp, line 333 
Traceback (most recent call last):
File "F:\IQ_option\OpenCV\run.py", line 5, in <module>
    cv2.imshow('image',img)
cv2.error: D:\Build\OpenCV\opencv-3.3.0\modules\highgui\src\window.cpp:333: error: (-215) size.width>0 && size.height>0 in function cv::imshow

The image:

My Code.

Answer

CodeHunter picture CodeHunter · Sep 14, 2017

The only reason for this to not work is that the file you are trying here isn't loaded properly in the imread command. The loaded image is non existent due to which it says that size.width and size.height > 0.

You can check if either the file is present in your specified location or not. Also, you might need to use double \\ while providing the full path of the .png file you want to display. Also, it might be that .png files are not supported here. You may try any other .jpg image file instead and retry. An example would be like this:

img = cv2.imread('C:\\Users\\fakepath\\Pictures\\Messi.jpg', cv2.IMREAD_COLOR)

Worked for me.