Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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. – James Milner May 9 '12 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 '12 at 13:13

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, change as desired

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


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

share|improve this answer
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. – kyle Jan 27 '14 at 19:50
That's interesting @kyle. What program are you closing and re-opening with where persistence doesn't work? – matt wilkie Jan 27 '14 at 20:21
It's a shortcoming (or not) of the geotiff format/driver, it looks like you submitted the ticket: – kyle Jan 27 '14 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. :) – matt wilkie Jan 28 '14 at 18:49

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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