Solving the Error – imread returns None. violating assertion !_src.empty() in function ‘cvtColor’ error

imread returns None. violating assertion !_src.empty() in function ‘cvtColor’ error is a Python error which happens when the path or the name of the image you are working with is not correct.

In this article I am going to show you guys why this error happens and how you can solve it, we will explore multiple possible solution in order to help you find the one which suits you the best.

Describing imread returns None. violating assertion !_src.empty() in function ‘cvtColor’ error

This error happens usually when the code is trying to open an image using an incorrect path to that image or using an incorrect name.

The error does not happen only in code that works with images but also with applications that involve video capture using a cam for example.

Here is an example of an application that may lead to this error.

We are using opencv  in Python and we want to open an image using imread like in the example bellow.

                                                                       #
frame = cv2.imread('frames/frame%d.tiff' % count)
frame_HSV= cv2.cvtColor(frame,cv2.COLOR_RGB2HSV)
                                                                       #

This is the resulting error we get.

                                                                       #
cv2.error: OpenCV(3.4.3) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
                                                                       #

Solution 1 : check if you have the correct path and the correct file name

As we said before the problem is usually either the incorrect name or path of the image.

So, the obvious fix is to double check the path of the image and see if you have made a mistake.

If not, please double check the name of the image.

Sometimes, a bug in your code will cause the problem and mess with the path, maybe you are not getting that path directly, maybe you are scraping or reading the path from a page, so make sure to print all the variables to the screen so you can verify where the error happens.

This also applies to the name of the image and not only the path.

Solution 2 : use skimage and imread

Sometimes, both the name and the path to the image are fine, in this case you can cv2 like in the following example. Replace the code bellow.

                                                                       #
cv2.imread(file_path)
                                                                       #

With the following code

                                                                       #
from skimage import io
img = io.imread(file_path)
                                                                       #

Solution 3 : if you are reading the images from a webcam

This solution is for people who are reading the images from a webcam for example or a phone connected to your computer.

The error is usually caused by a conflict that happens when your cam is open in another app.

For example you are running the Python code on google colab and it is using the camera while your standard camera application is open in your computer at the same time.

Please make sure to close one of the apps or websites using the camera.

Solution 4 : check the extension of the image

The last solution or should I say piece of advice is to check the extension of the image.

Sometimes you are loading the correct image with the correct path but the extension you are using is not the correct extension.

For example, you are confusing .jpg with .png.

If this helped you in any way consider donating to our Kofi account, there is a red button at the top of the page, Thanks.

Summing-up

This is the end of this article, this error is very common and does not need a lot of coding to get rid off, it usually takes a little bit of troubleshooting and investigating to find the root of the issue, I hope this helped, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/