Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

your problem is here:

output = (output * 255).astype(np.uint8)

numpy does not apply saturation when you convert to uint8, but does modulo overflow, as an example:

>>> a = np.array([1.1],dtype=np.float32)
>>> a * 255
array([280.5], dtype=float32)    <-- problem
>>> b = (a * 255).astype(np.uint8)
>>> b
array([24], dtype=uint8)    <-- problem
>>> np.clip(a*255,0,255)
array([255.], dtype=float32)

you should clip() your float array before the conversion:

output = np.clip(output * 255, 0, 255) # proper [0..255] range
output = output.astype(np.uint8)  # safe conversion