Using gdaldem hillshade, I get a one-band hillshade (download here, 2MB) :

 gdaldem hillshade crop_xl.tmp.tif Shadedrelief.tmp.tif -s 111120 -z 5 -az 315 -alt 60 -compute_edges

I currently can duplicate its band 1 (grey scale) into the band 2 (alpha) via :

 gdal_translate -b 1 -b 1 -co COMPRESS=LZW  -co ALPHA=YES ./Shadedrelief.tmp.tif ./Shadedrelief.2bands.tmp.tif

But what I want to do is to get the inverted value of band 1 into band 2. The equation is :

 y = -x + 255;  // where x is -b 1 's value, and y is -b 2 's value.

Given a tif hillshade as provided, how to get the inverted value of band 1 into band 2 ?

See also : How to conditionnally assign a new value to pixels of a raster image? , gdal_calc : fails with value 0?

1 Answer 1


First, use gdal_calc.py to invert the band :

gdal_calc.py -A Shadedrelief.tif --outfile=InvertedShadedrelief.tif --calc="255-A"

gdalbuildvrt helps to merge 2 files into a two band image (or a 2 band vrt), it takes the first band of each input into band 1 and band 2:

gdalbuildvrt -separate final.vrt Shadedrelief.tif InvertedShadedrelief.tif

gdal_translate helps to convert to tif and compress the file:

gdal_translate -co COMPRESS=LZW -co ALPHA=YES ./final.vrt ./final.tif
  • Thanks for the hint. I understand command 1, but It's my first contact with vrt and I don't understand the 2nd command. What are its input and output ? I would expect a kind of merge of input.tif -b 1 + result.tif -b 1 into a two bands final.tif -b 1 -b 2.
    – Hugolpz
    Apr 30, 2015 at 13:55
  • 1
    vrt is virtual raster file: you can manipulate it in most software as if it was a real 2 band image, but it is only a small xml file. Basically, if you want a single tif file, you can do : gdal_translate final.vrt final.tif
    – radouxju
    Apr 30, 2015 at 14:02
  • Tested, working, and happily validated !
    – Hugolpz
    Apr 30, 2015 at 14:58

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.