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?

  • 1
    Which colour are you wanting to make transparent? Also, colour can be pretty arbitary because it is based on a set of numbers between 0 - 255 on 3 scales. You'd have to specify the exact range of colour you wanted to make transparent. May 9, 2012 at 9:26
  • I want to make a specific colour - not a range of colours - transparent. A range would also be nice to have, but at the moment a specific colour will do. I specified that I am working with an RGB image, and it might be me simplifying stuff, but I assume that a colour is an RGB colour, thus a combination of values in all 3 bands. I apologise for any unclear asumptions :)
    – Chau
    May 9, 2012 at 13:13

2 Answers 2


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
  • 1
    cool! Although NODATA integrity is lost when passing to GTiff: in this case gdalinfo would report 123 as NODATA value for each band (GDAL 2.1.1 here)
    – Campa
    Dec 14, 2016 at 16:54

Herein lies a misunderstanding: "...that is done separately for each band and not for a specific colour."

"Each band" and "specific colour" are in fact the same thing. That is, it is the values in each band that, when combined together, make a specific colour. For example the RGB triplet 255,0,0 is the specific colour of pure red, comprised of band-red at the top of the range, 255, and band-green at the bottom, 0, ditto for band-blue.

Here's a simple gdal python script to change the nodata values for a specific image with 3 channels, a.k.a RGB:

from osgeo import gdal

input = 'd:/gisdata/test.tif'
dataset = gdal.Open(input, gdal.GA_Update)  # open the raster for writing

# 1 corresponds to Red channel, 2 for Green, 3 for Blue
R = dataset.GetRasterBand(1)
G = dataset.GetRasterBand(2)
B = dataset.GetRasterBand(3)

print 'Initial nodata values (RGB):\t', R.GetNoDataValue(), G.GetNoDataValue(), B.GetNoDataValue()

# set null value to the background page colour used on GIS.se, change as desired

print 'New nodata values (RGB):\t', R.GetNoDataValue(), G.GetNoDataValue(), B.GetNoDataValue()


D:\>python setnull.py
Initial nodata values (RGB):  None None None
New nodata values (RGB):      251.0 250.0 247.0

A more generalized script that can handle arbitrary numbers of bands and used as a commandline utility can be found my code swamp, as gdalsetnull.py

  • This won't work for GeoTiffs, at least persistently. GeoTiffs can't have different Nodata values for different bands. If you close the dataset and re-open it, all the no data values will be 247. You may need to wrap it with a vrt to get it to work.
    – user10353
    Jan 27, 2014 at 19:50
  • That's interesting @kyle. What program are you closing and re-opening with where persistence doesn't work? Jan 27, 2014 at 20:21
  • It's a shortcoming (or not) of the geotiff format/driver, it looks like you submitted the ticket: trac.osgeo.org/gdal/ticket/2083
    – user10353
    Jan 27, 2014 at 23:15
  • @kyle hah! that's funny, and embarrassing. I guess that means I'm earning my grey hairs, don't remember some of my own work. :) Jan 28, 2014 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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