Posted by & filed under QGIS.

If you work with raster data in GIS you straddle a kind of wierd world where you deal with imagery but you cannot really manipulate the imagery as you would say with the GIMP or Photoshop. Conversely, if you ever tried to open a large GIS raster dataset with the GIMP you may have noticed it really sucks at it. In this article I show a technique I used to apply simple image processing techniques commonly used in graphics apps like the GIMP to GIS raster data. Read on after the break…

I recently posted an article on creating coloured rasters with gdal which outlined a procedure for applying colour from polygonal areas to a raster.  I used the technique to produce a pretty looking hillshade map with height classes superimposed on it like this:

 

Our fancy relief map created using hsv_merge and other gdal tools

Our fancy relief map created using hsv_merge and other gdal tools

While the terrain map looks great, the colours are very dominant and it makes it difficult to see the trail data (the real purpose of the map) clearly.  There are a lot of great articles out there (e.g. this one) on the importance of contrast in graphic design, and I wanted to apply those principles to the cartographic outputs I was working on. In QGIS it is quite simple to desaturate an image by assigning a global transparency level to it. Here is the same map as above but with 70% transparancy assigned to it using the raster layer properties dialog in QGIS.

Achieving a 'washed out' effect in QGIS using global transparency (click to enlarge).

Achieving a 'washed out' effect in QGIS using global transparency (click to enlarge).

Things are never quite as simple as that though. The maps we are producing need to be printable using the QGIS Server (and from within QGIS desktop). For good printable output in QGIS, using raster transparency is a no-no at the moment because of some idiosyncrasies in the way that the print subsystem renders. Basically, the print system chops up the raster into possibly slightly overlapping tiles (due to rounding errors) which can result in a banding effect when two semi -transparent tiles are superimposed. So the long and the short of it is that although display purposes you can obtain a nice effect (as shown above), ideally we need to be able to create a new raster with that effect pre-applied.

Enter my discourse on why GIS are a kind of wierd platform for doing raster editing. In GIMP I would simply overlay a white layer and then use an overlay mode such as ‘screen‘, add etc. in order to get the desired effect. So I decided to write my own tool to do this. I used Frank Warmerdam’s hsv_merge.py script as inspiration and created a tool for applying lightening or darkening effects to a raster.  Frank’s implementation is nice in that it works with really big rasters since it processes the data in blocks – and my tool follows suite. You can download the tool from github:

https://github.com/timlinux/RasterProcessing

To use the tool, simply run it like this:

python gdal_lightener.py -a 180 input.tif  output.light180.tif

Here are the general usage notes:

usage: gdal_lightener.py [-q] [-of file_format] -a amount -m mode src_color dst_color
where
amount is the amount of lightening to apply [0-255] default 180
mode - darken, lighten or screen
src_color is a RGB or RGBA dataset
dst_color will be a RGB or RGBA dataset
Screen lower amount to get a darker image (closer to original)

In general you probably won’t see much difference between ‘screen’ and ‘lighten’ modes. And the ‘darken’ mode obviously makes the image darker – useful if you are trying to  overlay light coloured content with good contrast. Afterwards you can add the new layer to your QGIS project and it will have a ‘washed out’ appearance without having to apply any transparency to achieve the effect.

 

Our new raster with the 'screen' effect applied (-a 180) and no transparency enabled. Click to enlarge.

Our new raster with the 'screen' effect applied (-a 180) and no transparency enabled. Click to enlarge.

 

Eventually I will probably gather more little functions like this and create a QGIS plugin so that they are accessible directly from the GUI. Have fun!