BC Consulting


  Convert 1-band raster to
1-band paletted or 3-band RGB raster
V1.0.0 - 21 May 2012

This QGIS plugin is used to create a paletted or a RGB raster from an one-band raster file and its QGIS .qml file.

Please note: The required versions of QGIS for this plugin to work correctly are 1.7 or 1.8.

Distributed under the GPL licence

Use this plugin for:

An one-band real-valued raster file in QGIS can be 'coloured' using different colour palettes applied to different colouring methods.

Unfortunately, such a coloured raster layer cannot be saved as a coloured raster file outside of QGIS, well, up until now!.

Use this plugin to create a one-band paletted raster file or a 3-band RGB raster file, generally a GeoTIFF(*), that can be opened and viewed with any compliant viewer.

The parameters required are (1) the input one-band raster file name, (2) the QGIS .qml file generated by QGIS, (3) the output raster file name and (4) a flag to either generate a paletted raster file or a RGB (default) raster file.

Once installed the bccAddRColor plugin can be found in the 'Raster/Raster Colours' menu.

Please report any bug/problem/wish to the QGIS User mailing list [mailto: qgis - user at lists dot osgeo dot org] (remove any space and replace 'dot' with dot and 'at' with the at-sign).

Guided tour:

Prior to using the plugin, please generate the qml file from QGIS: select the input raster in the legend, right click and select 'Properties' then click of the 'Save style...' button to create the qml file.

The plugin user interface is shown below:

The input raster must be a 1-band raster having a QGIS colour palette. There is no point to use this plugin on GDAL paletted raster or any 3-band (RGB) rasters.
Enter the name (full path, e.g.: c:\path\to\the\file\file.tif) of the input raster in the text box or click on the 'choose' button to navigate to the file.

Then enter the name (full path) of the qml in the provided text box or click the 'choose' button to navigate to the file.

Enter the name (full path) of the output raster file or click the 'choose' button to navigate to the file. The output raster should have a GeoTIFF format (the plugin has not been tested with other output formats).

Then select the type of output raster you want: 1-band paletted (not recommended) or 3-band RGB (default).

And finally click on 'Colorize raster' to convert the 1-band raster to a colour raster.

Additionally you can check the 'Load raster into canvas' to load the output raster into QGIS after conversion.

Click on the 'Close' button to return to QGIS.



The three images below show:

Example of this plugin usage
in viewer
in viewer after conversion
1-band raster in image viewer
1-band raster in QGIS (with colormap - qml)
Converted RGB raster in image viewer

Below is the result of paletted conversion of the 1-band raster above displayed in IrfanView (note the different rendering in function of the raster type):

paletted conversion
Converted paletted raster in IrfanView image viewer.
Incorrect display of the raster.
The raster is in UInt16 format (bccAddRColor version < 0.3.0)

paletted conversion
Converted paletted raster in IrfanView image viewer.
Correct display of the raster.
Tthe raster is in Byte format (bccAddRColor version >= 0.3.0)

Note that paletted GeoTIFF files can be correctly opened in most GIS software but most image viewer software will have difficulties to correctly display such files.

Test data

In order to see the result of applying different colour schemes in QGIS on the resulting paletted/RGB raster, test data is available here and detailed below.

The test data is a small raster (geoTIF) shown below:

paletted conversion

This raster is imported in QGIS and a series of QML files are generated from it using all the different colouring methods available in QGIS (as at version 1.8): 'greyscale', 'pseudocolor', 'freak out' and 'colormap'. The first three methods use "stretch to min-max". Each method has an "Inverse color map" option, resulting in 16 QML files.

Then, we apply qml2gdal.py for each of the 16 options and we get:

QGIS (screen dump)
greyscale greyscale RGB greyscale paletted   greyscale paletted
greyscale inverse greyscale RGB inverse greyscale paletted inverse   greyscale paletted inverse
pseudocolor pseudocolor RGB pseudocolor paletted   pseudocolor paletted
pseudocolor inverse pseudocolor RGB inverse pseudocolor paletted inverse   pseudocolor paletted inverse
freak out freak out RGB freak out paletted   freak out paletted
freak out inverse freak out RGB inverse freak out paletted inverse   freak out paletted inverse
color map color map RGB color map paletted   color map paletted
color map inverse color map RGB inverse color map paletted inverse   color map paletted inverse


This plugin is a QGIS wrap around qml2gdal.py script.
qml2gdal.py can be run from the command line as follows:

    qml2gdal.py -if name -qml name -of name -RGB [0|1]
        -if name:   name of the input raster file (one-band raster only)
        -qml name:  name of the qml file of the input raster
        -of name:   name of the output raster file
-RGB [0|1]: 0 if output raster is paletted, 1 if it is RGB (default)

e.g.: For paletted output raster:
-if R:/__sandPad/dtm.tif -of R:/__sandPad/dtmP.tif -cpl R:/__sandPad/dtm.qml -RGB 0

e.g.: For RGB output raster:
-if R:/__sandPad/dtm.tif -of R:/__sandPad/dtmP.tif -cpl R:/__sandPad/dtm.qml -RGB 1

The algorithm used is as follows:

  1. Get the 1-band raster name, qml name & output raster name and type.
  2. Import qml file into an array.
  3. Generate array from the colour table:
    -> [ X, Y, I, R, G, B] values
    so that rules like:
    if raster_value > X and raster_value <= Y: raster_value = I can be applied. R,G,B are then used to create a GDAL palette or a colour triplet. Each index of the GDAL palette corresponds to each I in the raster.
  4. Open the raster to process and create a temporary raster.
  5. Open temp GeoTIFF raster of type UINT16 if paletted or UINT8 if RGB.
  6. Read the raster block by block and change the raster values according to
    the rules defined in 2) and set to the temp raster.
  7. Close temp raster and translate it to the output raster.

Version History

Version 1.0.0

  • Add capability to use qml files generated from 'greyscale', 'pseudocolor' and 'freak out' colouring methods.
  • Take into account the 'Inverse color map' flag when generating the paletted or RGB output raster file.
  • Correct input line parameter error (-cpl instead of -qml).
  • If a one-band raster is selected in the layer pane before launching the plugin then the qml file is automatically created if one with the same name of the raster does not exist.
  • 'Greyscale', 'pseudocolor' and 'freak out' palettes are generated using python code. This means that if the original C++ routines are changed, as will certainly be the case for QGIS V2.x, then this plugin will output paletted or RGB rasters having different colours than in the QGIS canvas.

Version 0.3.0

  • Adapt paletted raster type to either Byte or UInt16 depending on the number of colours in the palette.
  • Some code reorganisation.

Version 0.2.1

  • Clear info line before processing.
  • Add an example section to the help page (this page).
  • Change the standalone About dialog to a messagebox.

Version 0.2.0

  • If active layer is 1-band colormap raster then the input and qml boxes are filled with that layer info.
  • If filename box is not empty, clicking the 'choose' button sets the directory to the folder of the filename in the box.
  • Some code reorganisation.

Version 0.1.1

  • Correct version number mismatch.

Version 0.1.0

  • Original public release.
  • No testing done on output raster format other than GeoTIFF.


Some parts of the code are adapted from Franck Warmerdam's and Chris Yesson's python routines. They are here acknowledged for those lines of code. See the python file for copyright information.