[GIS] Convert specific RGB (GeoTIFF) color to transparent

gdalgeotiff-tiffpythonrgbtransparency

I have an RGB GeoTIFF file and I want to convert a specific RGB colour to transparent. I would like to use GDAL for doing this, but external Python scripts are also an option (though I haven't learned the art of Python scripting yet).

My thoughts are:

  1. Add a 4th band to my GeoTIFF file and fill with 255.
  2. Insert 0 into the 4th band where RGB colour == target colour

The first part I think I can solve using

gdalwarp -dstalpha source.tif target.tif

The second though I don't know how to solve. matt wilkie suggest using a Python script he wrote, but I think that is done separately for each band and not for a specific colour.

How can I assign a value to the 4th band of my TIFF when the RGB colour is equal to a specific RGB colour?

Best Answer

here's a better answer, use gdalbuildvrt with either srcnodata or vrtnodata flag:

gdalbuildvrt -srcnodata "123 231 67" outfile.vrt input.tif

If the next application in line doesn't understand .vrt, translate to a new tif:

gdal_translate outfile.vrt final.tif
Related Question