cv2.error: (-215:Assertion failed) !ssize.empty() / !_src.empty() (OpenCV)
The image you passed to resize or cvtColor is empty — because cv2.imread silently returned None. Check the read result before processing, and watch paths, working directories and non-ASCII characters.
Updated
The error
cv2.error: OpenCV(4.10.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4152: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
The cvtColor variant of the same disease:
cv2.error: OpenCV(4.10.0) ... error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
What it means
-215 is OpenCV's assertion-failure code, and !ssize.empty() / !_src.empty() translate to: "the input image is empty". Almost always, the emptiness began earlier — cv2.imread does not raise on failure; it returns None — and the error only surfaces at the next function that touches the non-image. The bug is at the load, not at the resize.
Why it happens
imread returns None for any of: a wrong path (including right-name-wrong-directory, the relative-path classic), a file that is not actually an image, a corrupt or zero-byte file, insufficient permissions — and, specific to Windows, paths containing non-ASCII characters, which imread historically fails to open. For videos, cap.read() similarly returns (False, None) at end of stream or when the codec is unsupported, and processing the None frame lands in the same place.
How to fix it
1. Check the read, immediately and loudly.
import cv2, os
img = cv2.imread(path)
if img is None:
raise FileNotFoundError(
f"imread failed: {path!r} (exists={os.path.exists(path)}, cwd={os.getcwd()})"
)The three facts in that message — path, existence, working directory — identify most causes on sight. File exists but still None? The file is not a readable image, or the path has non-ASCII characters.
2. For non-ASCII paths on Windows, decode from bytes.
import numpy as np
data = np.fromfile(path, dtype=np.uint8) # fromfile handles unicode paths
img = cv2.imdecode(data, cv2.IMREAD_COLOR)3. Build paths absolutely, not relative to wherever you launched from.
from pathlib import Path
IMG_DIR = Path(__file__).parent / "data" / "images"
img = cv2.imread(str(IMG_DIR / "photo.jpg"))Scripts run from IDEs, terminals and schedulers see different working directories; absolute construction removes the variable.
4. For video, honour the ret flag.
cap = cv2.VideoCapture("clip.mp4")
while True:
ret, frame = cap.read()
if not ret:
break # end of stream, not an error
process(frame)A capture that yields ret=False on the very first read means the file did not open — wrong path or missing codec support.
5. In dataset pipelines, filter unreadable files once, up front. One corrupt image among fifty thousand otherwise means a crash at a random epoch. A preflight scan that attempts imread on every file and quarantines the Nones converts that into a tidy list.
How to prevent it
Wrap image loading in one helper that raises with context on None, and use it everywhere — never bare imread. Treat "assertion failed, empty" in any OpenCV function as "walk backwards to the load" rather than debugging the function named in the error.
Related errors
- The function is not implemented (OpenCV GUI)
- No module named 'cv2'
- ValueError: cannot reshape array of size — the NumPy face of unexpected image contents
- Expected 4-dimensional input