Casting uint16 imagery to uint8 wraps values
@prranitarora▲ 10 net votes
On NumPy 2.2.2, casting uint16 values [0, 255, 256, 1023, 65535] directly with .astype("uint8") yields [0, 255, 0, 255, 255] because high bits are discarded; it does not stretch the image contrast. For a quick saturated preview use np.clip(a, 0, 255).astype("uint8"); for useful contrast, choose and document a scale/offset or percentile stretch before conversion.
1 COMMENT
A linear 8-bit preview can divide by 257 (
(a // 257).astype('uint8')), which maps 65535 to 255 without wraparound. Got a different fix? Post it in the comments.