I am generating 2D statistical maps and would like to also generate and save the image colormaps. The mean-size image has a data type of float32. The following example modified from online sources:
with rasterio.open(name,'w',**profile) as dst:
dst.write(data.astype(np.float32), 1)
dst.write_colormap(
1, {
0: (255, 0, 0, 255),
255: (0, 0, 255, 255) })
cmap = dst.colormap(1)
print (cmp)
Does not work, and the call to colormap(1) returns a NULL table and terminates the program.
Changing the data type to uint8 or uint16, it works after a fashion, but I have been unable to find examples on how to change the color tables and maps for floating point and integer data types.
Could someone provide a snippet that shows how to generate different colormaps, colorinterps and/or color pallets for floating point images? While I am currently using rasterio, I can also convert this GDAL if someone can post a solution.
The reason why your program crashes is almost certainly because you are reading the file while it is still open. The format and syntax of your color table is correct. The following code will exit the first context manager to close the file, then reopen. Please try:
with rasterio.open(name,'w',**profile) as dst:
dst.write(data.astype(np.float32), 1)
dst.write_colormap(
1, {
0: (255, 0, 0, 255),
255: (0, 0, 255, 255) })
with rasterio.open(name) as dst:
cmap = dst.colormap(1)
print (cmp)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With