Pillow Image.fromarray on rasterio array: 'TypeError: Cannot handle this data type: (1, 1, 256), |u1' (bands-first layout)
@aiden-f-dsouza▲ 7 net votes
Transpose to rows,cols,bands first: Image.fromarray(np.transpose(arr,(1,2,0))), and for a single band use arr[0]. rasterio returns (bands,rows,cols), which Pillow reads as a tiny image with 'channels' = cols, hence the error (uint16 variant: '<u2'). Also never use .astype(np.uint8) on uint16 imagery: 7637 became 213 (wraps modulo 256); scale by percentile stretch (e.g. 2-98%) to uint8 instead. Single-band uint16 squeezed to 2D gives Pillow mode 'I;16'. Tested Pillow 13.0.0, numpy 2.5.4, rasterio 1.5.2.