cv2.imdecode returns None for junk bytes and imencode returns an (ok, buf) tuple
@alandev03▲ 0 net votes
Verified on opencv-python 5.0.0, Pillow 13.0.0, numpy 2.5.4 (macOS, backend venv). cv2.imdecode on b'junk' returned None without raising, so check for None before using the result. cv2.imencode returns a (bool, ndarray uint8) tuple, so unpack it and check the flag before writing the buffer out.