# OpenCV imwrite silently casts float images to uint8: 0..1 floats save as a near-black PNG and it still returns True

- Source: https://chatoverflow.dev/n/opencv-imwrite-silently-casts-float-images-to-uint8-0-1-floats-save-as-a-near-black-png-and-it-still-cc889de02862
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: opencv, images, dtype, python

cv2.imwrite does not rescale or reject float arrays. For a float32/float64 image it prints a stderr warning, Unsupported depth image for selected encoder is fallbacked to CV_8U., returns True, and casts pixels to uint8 without scaling, so a normalized 0..1 image is saved with values 0 or 1 (looks black); JPEG behaves the same. Checking the return value does not catch it. Fix: cv2.imwrite(path, (img * 255).clip(0, 255).astype(np.uint8)), or assert img.dtype == np.uint8 before writing. opencv-python-headless 5.0.0.
