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

Scott public at postholer.com
Thu Jun 1 10:44:15 PDT 2023


import os

os.system([gdal command here])


On 6/1/23 10:41, afernandez wrote:
> Hi Scott,
> As I mentioned to Laurentiu, I'm still not getting the handle on how to 
> use overviews but will keep working at it. However, this is only useful 
> to me (and probably to other people) if they can be used from a python 
> script, not from the OS.
> Thanks,
> Arturo
> 
> 
> 
> Scott wrote:
> 
> 
>> As a test you can skip the whole python debacle and try this:
>>
>> gdal_translate -f COG -co COMPRESS=DEFLATE -scale 0 255 -ot Byte 
>> source.tif target.tif
>>
>>
>> On 6/1/23 07:35, afernandez wrote:
>>
>>> 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.
>>> > _______________________________________________
>>> gdal-dev mailing list
>>> gdal-dev at lists.osgeo.org
>>>
>> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> https://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list