1

I have a set of single-band gray-scale rasters that I want to save as a rendered GeoTIFF/image that is colored according to a specific color ramp.

For example, if I had some NDVIs that range from a minimum and maximum value of -1 to 1 and I want them colored according to a ramp that would be similar to this: 0 (red) -> 0.5 (yellow) -> 1 (green).

I know how to do this within QGIS and ArcMap but I am looking to do it straight up in open source Python. I have looked into gdal and rasterio but I don't think I've found exactly what I need. Especially when it comes to saving them with the applied color ramp and forcing a min/max value of the ramp.

Are there any resources I can be pointed to?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 2, 2020 at 19:27

4 Answers 4

1
import rasterio
import matplotlib
from matplotlib import pyplot as plt
import matplotlib.colors
rasterfile = r"/home/bera/Desktop/gistest/nvdi.tif"
dataset = rasterio.open(rasterfile, mode="r")
array = dataset.read(1)
norm=plt.Normalize(0, 0.6) #My nvdi have values from ~ 0-0.6
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","yellow","limegreen"])
plt.imshow(array, cmap=cmap, norm=norm)
plt.colorbar()

enter image description here

answered Feb 20, 2024 at 17:44
0

Follow the very very recent development in here https://github.com/OSGeo/gdal/pull/3133. The pull request has been submitted two days ago but is has not been accepted yet. From the description of the PR

What does this PR do?

This PR adds option to attach color table from a supported file and to more extent options (union, intersection, custom). The related functions were implemented in #3131.

Because the pull request contains new complete python scripts you may be able to just capture them and try right away. However, they may require some components from the previous PR from the same author.

answered Nov 2, 2020 at 20:34
-1

I think using gdaldem solved my issue for now using the color-relief option

https://gdal.org/programs/gdaldem.html

answered Nov 3, 2020 at 15:08
-1

Look this:

ndvi = (nir - red) / (nir + red)
ndvi[ndvi > 1] = 1
ndvi[ndvi < -1] = -1
ndvi
answered Dec 23, 2022 at 23:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.