Color interpretation

Color interpretation of raster bands can be read from the dataset

>>> import rasterio
>>> src ="tests/data/RGB.byte.tif")
>>> src.colorinterp[0]
< 3>

GDAL builds the color interpretation based on the driver and creation options. With the GTiff driver, rasters with exactly 3 bands of uint8 type will be RGB, 4 bands of uint8 will be RGBA by default.

Color interpretation can be set when creating a new datasource with the photometric creation option:

>>> profile = src.profile
>>> profile['photometric'] = "RGB"
>>> with"/tmp/rgb.tif", 'w', **profile) as dst:
...     dst.write(

or via the colorinterp property when a datasource is opened in update mode:

>>> from rasterio.enums import ColorInterp
>>> with'/tmp/rgb.tif', 'r+', **profile) as src:
...     src.colorinterp = [

And the resulting raster will be interpretted as RGB.

>>> with"/tmp/rgb.tif") as src2:
...     src2.colorinterp[1]
< 4>

Writing colormaps

Mappings from 8-bit (rasterio.uint8) pixel values to RGBA values can be attached to bands using the write_colormap() method.

import rasterio

with rasterio.Env():

    with'tests/data/shade.tif') as src:
        shade =
        meta = src.meta

    with'/tmp/colormap.tif', 'w', **meta) as dst:
        dst.write(shade, indexes=1)
            1, {
                0: (255, 0, 0, 255),
                255: (0, 0, 255, 255) })
        cmap = dst.colormap(1)
        # True
        assert cmap[0] == (255, 0, 0, 255)
        # True
        assert cmap[255] == (0, 0, 255, 255)['open', '/tmp/colormap.tif'])

The program above (on OS X, another viewer is needed with a different OS) yields the image below:

Reading colormaps

As shown above, the colormap() returns a dict holding the colormap for the given band index. For TIFF format files, the colormap will have 256 items, and all but two of those would map to (0, 0, 0, 0) in the example above.