cv2.error (-215:Assertion failed) !_src.empty() (tested)

(-215:Assertion failed) !_src.empty() in function 'cvtColor' (or !ssize.empty() in resize) means the image you passed is empty, almost always because cv2.imread() returned None for a wrong path. imread does not raise; check if img is None right after it.

1. Reproduce it

b1.py:

import cv2, numpy as np

def attempt(label, fn):
    try:
        fn()
        print(f"{label}: OK")
    except cv2.error as ex:
        print(f"{label}:")
        for l in str(ex).splitlines():
            if l.strip():
                print("  " + l.strip())

img = cv2.imread("missing.jpg")              # wrong path: no exception, just None
print("cv2.imread('missing.jpg') ->", img)

attempt("cvtColor(None)", lambda: cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
attempt("resize(None)", lambda: cv2.resize(img, (100, 100)))
empty = np.zeros((0, 0, 3), np.uint8)
attempt("resize(empty array)", lambda: cv2.resize(empty, (100, 100)))
gray = np.zeros((50, 50), np.uint8)
attempt("cvtColor(gray, BGR2GRAY)", lambda: cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY))
f64 = np.zeros((50, 50, 3), np.float64)
attempt("cvtColor(float64 image)", lambda: cv2.cvtColor(f64, cv2.COLOR_BGR2GRAY))

Output (opencv-python-headless 5.0.0, numpy 2.5.4, Python 3.13.5, Linux):

[ WARN:0@0.007] global loadsave.cpp:278 findDecoder imread_('missing.jpg'): can't open/read file: check file path/integrity
cv2.imread('missing.jpg') -> None
cvtColor(None):
  OpenCV(5.0.0) /io/opencv/modules/imgproc/src/color.cpp:199: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
resize(None):
  OpenCV(5.0.0) /io/opencv/modules/imgproc/src/resize.cpp:4217: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
resize(empty array):
  OpenCV(5.0.0) /io/opencv/modules/imgproc/src/resize.cpp:4217: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
cvtColor(gray, BGR2GRAY):
  OpenCV(5.0.0) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:92: error: (-15:Bad number of channels) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<1>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
  > Invalid number of channels in input image:
  >     'VScn::contains(scn)'
  > where
  >     'scn' is 1
cvtColor(float64 image):
  OpenCV(5.0.0) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:94: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<1>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
  > Unsupported depth of input image:
  >     'VDepth::contains(depth)'
  > where
  >     'depth' is 6 (CV_64F)

2. The fix: check what imread returned

import cv2, numpy as np
from pathlib import Path

def load(path):
    img = cv2.imread(str(path))
    if img is None:
        raise FileNotFoundError(f"cv2.imread could not read {Path(path).resolve()}")
    return img

cv2.imwrite("ok.png", np.full((20, 30, 3), 255, np.uint8))
img = load("ok.png")
print("shape", img.shape, "dtype", img.dtype)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
print("gray", gray.shape)
f64 = np.zeros((50, 50, 3), np.float64)
print("float64 -> float32:", cv2.cvtColor(f64.astype(np.float32), cv2.COLOR_BGR2GRAY).dtype)
try:
    load("missing.jpg")
except FileNotFoundError as ex:
    print("FileNotFoundError:", ex)
shape (20, 30, 3) dtype uint8
gray (20, 30)
float64 -> float32: float32
[ WARN:0@0.010] global loadsave.cpp:278 findDecoder imread_('missing.jpg'): can't open/read file: check file path/integrity
FileNotFoundError: cv2.imread could not read /tmp/cv405/missing.jpg

Related: cv2.imshow error: The function is not implemented.

Frequently asked questions

Why does cv2.imread return None?

The file could not be opened or decoded, usually a wrong or relative path. imread only prints a warning and returns None instead of raising.

What does !ssize.empty() in function resize mean?

cv2.resize got an empty image: None from imread, or a zero-size crop.

How do I fix Bad number of channels in cvtColor?

The image already has 1 channel; skip COLOR_BGR2GRAY when img.ndim == 2.

Written with AI assistance. Measured on 2026-10-23 with opencv-python-headless 5.0.0, numpy 2.5.4 and Python 3.13.5 on Linux. Scripts ran verbatim and the output was pasted unedited. PlainTables is our extension; OpenCV is free.