Solving Opencv Assertion failure error : size.width>0 && size.height>0 in function imshow

Opencv Assertion failure error : size.width>0 && size.height>0 in function imshow is an error which occurs when you try to read and display an image using Opencv and python.

Today I will be explaining why this error is taking place and how to solve it.

Exploring Opencv Assertion failure error : size.width>0 && size.height>0 in function imshow

This is an error which occurs when you try to read and display an image using Opencv and python.

Please double check the error message in this blog post so you can avoid mixing between different python issues.

                                                                       #
window.cpp:...: \
error: (...) size.width>0 &&  size.height>0 in function imshow.
                                                                       #

Bellow is a number of tested methods that I have tried and that have worked for me.

Solution 1 : You think you have the correct Path to the image but you do not, also Make sure the name with image is correct when opening an image with Opencv

The error might occur for many reasons when you try to read or/and display an image using Opencv with python.

One of those reasons is Path related problems, please make sure the path to the image is correct.

Also, make sure you have the leading / in the path of the image

Also, make sure the name of the image is correct. Try the method bellow if this one does not work.

Solution 2 : use the Python raw string ‘r’ or ‘R’ prefix before the Path of the image when using cv2.imread

Another option that you might want to try is using the ‘r’ or ‘R’ prefix in Python , also known as a Python raw string.

The Python raw string treats the backslash as a literal character. We do not want the string to be treated as an escape character.

                                                                       #
myimg = cv2.imread(r'C:\home\Git\OpenCV\resources\myimage.png')
                                                                       #

Try this method and if it works then great, you can try the method bellow if this one does not work.

Solution 3 : install pypng library

Something that happens to a lot of people when working with png images is that Opencv cannot read the images

PyPNG is a library that allows PNG images to be read and written with pure Python. You can install pypng using the pip package manager.

                                                                       #
pip install pypng
                                                                       #

I hope this guide solved your problem, thank you for reading. Thank you for reaching the end of this article.

Summing-up : 

Guys, this has been my best attempt at helping you understand and solve this issue. I hope you found a solution which suits your needs.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/