I would like to change the rgb-value of raster-tiffs from 0,0,0 to 1,1,1. Is there a commandline-tool that can change the rgb-values of GeoTIFF files?

At the moment I use irfanview to change the color. As long as I keep the TFW-file of the GeoTIFF but do change the color for many raster files is not very conveinient.


you can use Orfeo Toolbox, a c++ library with a lot of usefull applications. In your case, otbcli_BandMathX would do the job in one line:

otbcli_BandMathX -il input.tif -out output.tif -exp "((im1b1==0) and (im1b2==0)and (im1b3==0))?{1,1,1}:im1"

That is: if the three bands of your raster are equal to zero, then write a pixel of value [1,1,1] otherwise write the value of the first image. More info about this tool here. It is worth noting that you can use gdal properties for writing the image based on what they call extended file names, e.g. -out "output.tif?&gdal:co:COMPRESS=LZW&gdal:co:TILED=YES"

| improve this answer | |

gdal_calc.py will work, but it needs a few steps as it will only output a single band:

# Extract the bands with gdal_calc and edit the value of each band
gdal_calc.py -A in.tif -A_band=1 -B in.tif -B_band=2 -C in.tif -C_band=3 --calc  "A+((A*B*C)==0)" --outfile=outA.tif
gdal_calc.py -A in.tif -A_band=1 -B in.tif -B_band=2 -C in.tif -C_band=3 --calc  "B+((A*B*C)==0)" --outfile=outB.tif
gdal_calc.py -A in.tif -A_band=1 -B in.tif -B_band=2 -C in.tif -C_band=3 --calc  "C+((A*B*C)==0)" --outfile=outC.tif

# merge the out files
gdal_merge.py -separate -o out.tif outA.tif outB.tif outC.tif

This is not very performant, so if anyone has a better idea ... Maybe an own python solution would work better.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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