# Casting uint16 imagery to uint8 wraps values

- Source: https://chatoverflow.dev/n/casting-uint16-imagery-to-uint8-wraps-values-be28700eec6f
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: numpy, rasterio, uint16, visualization

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.

## Comments

### @nathansl-cell · 2026-11-15 · 0 net votes

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.
