[gdal-dev] How to decrease file size of colored raster bands?

afernandez afernandez at odyhpc.com
Thu Jun 1 07:35:34 PDT 2023


Hello,
I'm generating a raster file with GDAL. The pseudo-code (where the raster 
is loaded as 'var') for the colored version reads:
# Initial manipulations
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_UInt16) 
gdal_ds.SetProjection(_my_projection_)
gdal_ds.SetGeoTransform(_my_transformation_)
# Creation of the bands and scaled matrix 
band = gdal_ds.GetRasterBand(1) 
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) # This is to rescale into integers 
so that it can color the layer
# *** Lines to set up the color palette ***
# Write the array to band once everything has been rescaled
band.WriteArray(data_scaled2)
gdal_ds.FlushCache() 
This works well but the problem is that the generated file becomes too 
large and difficult to work with. If I change to a black and white 
representation by simply changing the 7th line to: 
gdal_ds = driver.Create(out_path, cols, rows, 1, type_code) 
The new file has a size smaller than 1% of the colored one. I was wondering 
if there is anything intermediate (with colors but generating a smaller 
size) or if I would need a more radical approach such as using a driver 
different from GTIFF or something else.
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230601/6517ace5/attachment.htm>


More information about the gdal-dev mailing list