Search code examples
gdalgeotiffsatellite-image

gdal_translate 8bits (Byte format), how to handle nodata-values


I have a satellite image in GTiff with a range of 0 - 65535 which I need to convert to Byte format (0-255).

using: gdal_translate [-a_nodata 0 and -scale 0 65535 0 255] -ot Byte infile.if outfile.tif

This works fine but I get a lot of pixels which are rounded down (truncated) to 0, which is my nodata value, this means that they become transparent, when it is visualized.

I have tried playing around with -a_nodata 0 and -scale 1 65535 0 255, but I haven't been able to find a solution that works for me.

What I'm looking for is getting 0 as nodata and 1-255 as the data range.


Solution

  • If anybody else stumbles onto this, I would just like to post the solution I found.

    The routine gdal_calc.py, which enables one to use Python functions from e.g. numpy and math, can do the trick easily.

    gdal_calc.py -A inputfile.tif --outfile=outputfile.tif --calc="A/258+1" --NoDataValue=0
    

    Then one just needs to convert it to Byte format by gdal_translate og gdalwarp (if one needs to re-grid the data as well).