Menu
  • About us
    • Company
    • Team
    • Career
    • References
    • Partners
    • Contact
  • Products
    • Earth Observation metadata enhancer
    • Temperaturdaten
    • LST-Data Client
    • Trainings
  • Services
    • actinia – geoprocessing in the cloud
    • Geospatial and EO data analysis
    • CORONA spy satellite data
    • maps.mundialis
    • Web Map Services
    • GIS Development
    • Open Source GIS
  • Markets
    • Remote Sensing for Agriculture
    • Satellite Images for Forest and Land Cover Restoration
    • Glass fibre route planning – FTTH
    • Copernicus and Sentinel
  • News & Blog
    • News
    • Blog
    • Satellite image of the month
  • English English
  • Deutsch Deutsch
  • About us
    • Company
    • Team
    • Career
    • References
    • Partners
    • Contact
  • Products
    • Earth Observation metadata enhancer
    • Temperaturdaten
    • LST-Data Client
    • Trainings
  • Services
    • actinia – geoprocessing in the cloud
    • Geospatial and EO data analysis
    • CORONA spy satellite data
    • maps.mundialis
    • Web Map Services
    • GIS Development
    • Open Source GIS
  • Markets
    • Remote Sensing for Agriculture
    • Satellite Images for Forest and Land Cover Restoration
    • Glass fibre route planning – FTTH
    • Copernicus and Sentinel
  • News & Blog
    • News
    • Blog
    • Satellite image of the month
  • English English
  • Deutsch Deutsch

Extract and manipulate raster color tables with GDAL


27. June 2017 | Category blog

While working on a project to process and upload raster data to GeoServer, several challenges occurred. One task was to manipulate the color table of raster files for own usage.

Geospatial Data Abstraction Library (GDAL)

GDAL offers a convenient utility to change the look of raster files using color tables. The utility gdaldem color-relief is normally used to colorize a height ramp of a digital elevation model, but can also be used to generally change the appearance of the raster files:

Shell
1
gdaldem color-relief inputfile color_table.txt outputfile

To manipulate an already existing color table, we first needed to extract this information. The command gdalinfo can be used in Bash to show the metadata of a raster file, but it is not possible to extract the color table in a convenient way.

GDAL Python utilities

With the GDAL Python utilities one can however use a series of functions to extract this information and save it to a text-file. GetRasterBand(1) retrieves the information of the first band and band.GetRasterColorTable() extracts for you the color table saved along with it. The first band of the raster files be for example of the type Palette and owns an 8-bit color table with 8*8=256 different entries. The function GetColorEnrty() then can read each of the 256 RGB-codes. So we just need to create a new text file and copy the entry-number (i) and the corresponding R-, G-, B-code (sEntry) from it:

Python
1
2
3
4
5
6
7
8
9
10
11
fn   = gdal.Open(filename)
band = fn.GetRasterBand(1)
ct   = band.GetRasterColorTable()
f    = open("rgb_color.txt", 'w+')    
       for i in range(ct.GetCount()):
          sEntry = ct.GetColorEntry(i)
          f.write( "  %3d: %d,%d,%d\n" % ( \
              i, \
              sEntry[0],\
              sEntry[1],\
              sEntry[2]))

color table

This text-file which has been created above contains 256 RGB-entries and looks as follows for our example:

Shell
1
2
3
4
5
6
7
    0: 255,255,255
    1: 153,255,179
    2: 230,255,204
    3: 191,242,128
    4: 230,230,204
    […]
    255: 255,255,255
Here the value zero (0) refers to white, the first entry corresponds to the color mint green and the second color is called cream. A useful webpage to translate these rgb-codes to verbal color names can be found here.

The sed tool

The program sed is a useful tool in Bash to change the color table. With it one can manipulate strings and also text files. To manipulate the colors in the raster file you have to write down the string you want to replace; here as a comma separated RGB-code. A colon “:” is used in this example to distinguish it from the new code. So if you just want to change the color white to black, write down 255,255,255:000,000,000 like in the example below:

Shell
1
sed -i -e "s:255,255,255:000,000,000:g" rgb_color.txt

If you want to change more than one color, a for loop would be suitable. Just write down the colors you want to change and save them in a variable, here called rgb_list and use the for to replace the colors with your desired ones:

Shell
1
2
3
4
rgb_list="191,242,128:034,139,034 255,153,179:255,000,000"
for i in  ${rgb_list}; do
   sed -i -e "s:${i}:g" rgb_color.txt
done

gdaldem

In a final step, we use gdaldem to manipulate the raster file with the help of our newly created rgb_color.txt and save it as a virtual dataset or as a GeoTiff:

Shell
1
gdaldem color-relief -of VRT input.tif rgb_color.txt rgb_output.vrt

Example using a digital topographic map

The maps show the border region between Germany and Austria near Salzburg. The original digital topographic map (DTK100) is shown on the left side. On the right hand-side, you can see the newly created map. Forests are shown with a darker green (034,139,034) instead of a light one (191.242,128). Urban areas are symbolized with the color red (255,000,000) instead of pink (255,153,179).

The script can be found on GitHub and is available for public usage.

Source: Digitale Topographische Karte 1:100.000, Bayrische Vermessungsverwaltung, 2017 https://geoportal.bayern.de/geodatenonline/seiten/wms_tk100

Tags: color table, GDAL, python

One thought on “Extract and manipulate raster color tables with GDAL”

  • Vincenzo says:
    Monday March 4th, 2019 at 12:37 PM

    I’m approaching naw in the script and I’m trying to select many multi-band files from a folder and extract a single band from the file, putting everything in an output folder with the single-band transformed files.
    Currently I concluded the following script:

    rlayer=”C:/…./file.ecw”
    rout=”C:/…./folder”
    import glob, os
    from osgeo import gdal

    driver = gdal.GetDriverByName(“rlayer”)
    tDs =gdal.Translate()
    ds_in = gdal.Open(“.ecw”)
    array = ds_in.GetRasterBand(3).ReadAsArray()
    band = tDs.GetRasterBand(1)
    band.WriteArray(array)
    band.FlushCache()

    I do not understand where it’s wrong

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow Us

Contact

mundialis GmbH & Co. KG
Kölnstrasse 99
53111 Bonn

Phone: +49 (0)228 / 387 580 80
Fax: +49 (0)228 / 962 899 57

E-Mail: info [at] mundialis [dot] de

Latest News

  • ++ Eine neue Heimat für Actinia ++

    Tuesday January 24th, 2023
  • Positive towards the future – Management changes at mundialis

    Tuesday October 4th, 2022
  • Recap of FOSS4G 2022 in Florence

    Friday September 16th, 2022
  • FOSS4G 2022 in “La Bella” – Greetings from Florence!

    Thursday July 14th, 2022
  • VALE project successfully completed

    Tuesday June 21st, 2022
View all News

Blog

  • Satellite image of the month – January – Yalu River (People’s Republic of China and Democratic People’s Republic of Korea) 2. January 2023
    Yalu River – People’s Republic of China and Democratic People’s ...
  • Satellite image of the month – December – Super Pit gold mine (Australia) 1. December 2022
    Super Pit gold mine – Australia, recorded by the Sentinel-2A ...
  • Satellite image of the month – November – Kufra-Oases (Libya) 1. November 2022
    Kufra-Oases – Libya, recorded by the Sentinel-2A satellite on March ...
View all Blog Posts

Copyright © 2015-2021 mundialis GmbH & Co. KG

Imprint Privacy

Theme created by PWT. Powered by WordPress.org