Skip to content Skip to sidebar Skip to footer

Python Opencv Error While Using Cv2.imread()

import cv2 import numpy as np #load color of an image in grayscale img1 = cv2.imread('Tarun.jpg',0) cv2.imshow('Hey its me Tarun yellogi',img1) error: C:\projects\opencv-python

Solution 1:

img1 = cv2.imread('Tarun.jpg',0)

As you know, imread reads in the specified image. With just the filename, as here, python tries to read the image from the current working directory. If the file does not exist, no error is reported and img1 will have a value of None.

That this line:

cv2.imshow('Hey its me Tarun yellogi',img1)

returns an error of:

error: C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow

means that the image specified in the imshow command (i.e. img1) has a width and/or a height of 0. What the error message is saying is that the image in function imshow must have a size where the width is greater than zero and the height is greater than zero.

You can check the image size with img1.shape. In this case you will probably get an error message AttributeError: 'NoneType' object has no attribute 'shape'. You can further check if img1 is None with img1 == None. If you get a response of True then you will know that img1 does not contain image data.

TL;DR

The file 'Tarun.jpg' was not found thus img1 == None.

Solution 2:

  • These kind of error occur when the path to the image is wrong
  • Or the image does not exist
  • Or the image is corrupted
  • If the image is not in the current directory then you have to mention the absolute path to the image
  • Use os module to make the path work in any other machine instead of hard-coding the absolute path
  • But this error message means that imshow() is not getting the right image to display
  • I would check the shape and the values of the image to make sure that the image is not corrupt or image is not None: print(img1.shape) and print(img1)
  • If the image with the path does not exist then cv2.imread() returns None
  • The error also indicates that the width and heights of the image is zero which is not possible

Solution 3:

I think you can try this in reading the pictures in a loop to prevent the pause by Nonetype error! It helps me in my case(Some jpg files is Nonetype in my case)

if img is None:
        continue

Solution 4:

If the image is not in the current directory then you have to mention the absolute path to the image.

Use os module to make the path work in any other machine instead of hard-coding the absolute path.

But this error message means that imshow() is not getting the right image to display. I would check the shape and the values of the image to make sure that the image is not corrupt.

Post a Comment for "Python Opencv Error While Using Cv2.imread()"