[gdal-dev] Why is color palette changed when the bands are plotted?

afernandez afernandez at odyhpc.com
Sun Sep 10 10:26:45 PDT 2023


Hello,
I'm generating a raster file with GDAL in QGISv3.30. The pseudo-code (with 
the raster data loaded as 'var') reads:
dims = var.dimensions
shape = var.shape
driver_name = 'GTIFF'
driver = gdal.GetDriverByName(driver_name) 
np_dtype = var.dtype
type_code = gdal_array.NumericTypeCodeToGDALTypeCode(np_dtype)
gdal_ds = driver.Create(_my_path_, cols, rows, 1, gdal.GDT_Byte) 
gdal_ds.SetProjection(_my_projection_)
gdal_ds.SetGeoTransform(_my_transformation_)
# Creation of the bands and scaled matrix 
band = gdal_ds.GetRasterBand(1) 
band.SetNoDataValue(no_data)
band.SetDescription(time_step)
data = var[_chosen_index_]
data = ma.getdata(data)
data_scaled = np.interp(data, (data.min(), data.max()), (0, 255))
data_scaled2 = data_scaled.astype(int) 
colors = gdal.ColorTable()
colors.CreateColorRamp(0, (165, 0, 35), 18, (201, 34, 39))
colors.CreateColorRamp(18, (201, 34, 39), 36, (226, 74, 51))
colors.CreateColorRamp(36, (226, 74, 51), 55, (244, 118, 71))
colors.CreateColorRamp(55, (244, 118, 71), 73, (250, 163, 91))
colors.CreateColorRamp(73, (250, 163, 91), 92, (253, 200, 118))
colors.CreateColorRamp(91, (253, 200, 118), 109, (254, 231, 152))
colors.CreateColorRamp(109, (254, 231, 152), 127, (254, 254, 189))
colors.CreateColorRamp(127, (254, 254, 189), 145, (231, 245, 161))
colors.CreateColorRamp(145, (231, 245, 161), 163, (198, 230, 127))
colors.CreateColorRamp(163, (198, 230, 127), 181, (162, 216, 104))
colors.CreateColorRamp(181, (162, 216, 104), 200, (114, 194, 101))
colors.CreateColorRamp(200, (114, 194, 101), 218, (64, 170, 90))
colors.CreateColorRamp(218, (64, 170, 90), 236, (20, 140, 75))
colors.CreateColorRamp(236, (20, 140, 75), 255, (0, 94, 42))
band.SetRasterColorTable(colors)
band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)
band.WriteArray(data_scaled2)
gdal_ds.FlushCache()
This has been working mostly well except when the color palette is changed 
to mostly greenish with some light yellow. I have been trying to figure out 
what is triggering the change of the color palette for the last 2 weeks 
(initially thinking that it'd be a small thing) but have come up empty. I 
have checked out all variables and types, but everything looks like the 
same whether it uses the given color ramp or the greenish palette. I was 
wondering if anyone has run into the same problem or has any suggestion 
because I really cannot find an explanation. 
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230910/9174ef08/attachment.htm>


More information about the gdal-dev mailing list