I have a performed band calculation to calculate the NDVI grayscale. It has float based values on a single band, between 0.0 and 1.0 respectively. I am attempting to colorize the single band via applying a ColorRamp, with my end goal being able to generate a colored Tif that doesn't require mapping software to visualize the colors. I believe the method approach I need to take is to apply a color ramp; however, it appears the ColorRamp only accepts integer values for the ranges though. The documentation for the method states this:

int GDALColorTable::CreateColorRamp (int nStartIndex, const GDALColorEntry * psStartColor, int nEndIndex, const GDALColorEntry * psEndColor)

How do I apply the ColorRamp using the float based values?

Here is what I have attempted thus far:

colors = gdal.ColorTable()
colors.CreateColorRamp(0.0, (82,44,15), 0.25, (255,128,44))
colors.CreateColorRamp(0.25, (255,128,44), 0.5, (255,255,61))
colors.CreateColorRamp(0.5, (255,255,61), 0.75, (0,255,52))
colors.CreateColorRamp(0.75, (0,255,52), 1.0, (0,53,12))

ds = gdal.Open(f"{path}NDVI.tif", gdal.gdalconst.GA_Update)
band = ds.GetRasterBand(1)

Do I need to perform a gdal.Translate to get the source image from float to 0-255 range?

1 Answer 1


Start and end indexes should be integer and color should be (c1, c2, c3, c4). Docs Link

colors.CreateColorRamp(1, (82, 44, 15, 255), 2, (255, 128, 44, 255))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.