Search code examples
pythonpython-2.7opencv

Python openCV: Cannot show an image


I'm using python opencv and I'm trying to import an image and show it but I get the following error:

import numpy as np
import cv2
img = cv2.imread('messi5.jpg',0)
cv2.imshow('image',img)
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, fi
le C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\highgui\src\
window.cpp, line 271
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\high
gui\src\window.cpp:271: error: (-215) size.width>0 && size.height>0 in function

enter image description here


Solution

  • In [1]: a = None
    In [2]: type(a)
    Out[2]: NoneType
    In [3]: import cv2
    In [4]: img = cv2.imread('messi.jpg',0)
    In [5]: type(img)
    Out[5]: numpy.ndarray
    In [6]: img = cv2.imread('messii.jpg',0)
    In [7]: type(img)
    Out[7]: NoneType
    

    Here your type of img is NoneType. So,It's problem with the miss spell or not existing of the image. Please check the spelling or existing of file.