3
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\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow
2
  • next to the .py is the image? Commented Jun 20, 2018 at 0:53
  • Your img1 is empty. Add the path to the Tarun.jpg, if it is not in your working directory. Commented Oct 28, 2020 at 16:37

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Unexplained downvotes... I get tired of having points in my answers incorporated into other answers with a coincident downvote...
1
  • 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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.