Skip to content

Get/set raster band color interpretation #100

Closed

Description

@sgillies

In GDAL's data model, bands may have a color interpretation. The default is "undefined". The value can be set or get by using GDALSetRasterColorInterpretation or GDALGetRasterColorInterpretation.

/*! Types of color interpretation for raster bands. */
typedef enum
{
    GCI_Undefined=0,
    /*! Greyscale */                                      GCI_GrayIndex=1,
    /*! Paletted (see associated color table) */          GCI_PaletteIndex=2,
    /*! Red band of RGBA image */                         GCI_RedBand=3,
    /*! Green band of RGBA image */                       GCI_GreenBand=4,
    /*! Blue band of RGBA image */                        GCI_BlueBand=5,
    /*! Alpha (0=transparent, 255=opaque) */              GCI_AlphaBand=6,
    /*! Hue band of HLS image */                          GCI_HueBand=7,
    /*! Saturation band of HLS image */                   GCI_SaturationBand=8,
    /*! Lightness band of HLS image */                    GCI_LightnessBand=9,
    /*! Cyan band of CMYK image */                        GCI_CyanBand=10,
    /*! Magenta band of CMYK image */                     GCI_MagentaBand=11,
    /*! Yellow band of CMYK image */                      GCI_YellowBand=12,
    /*! Black band of CMLY image */                       GCI_BlackBand=13
} GDALColorInterp;

One way to implement this would be to add a colorinterp(bidx) method to the reader class and a set_colorinterp(bidx, value) method to the updater class. As with the existing tags(bidx) method.

cc @kapadia

Activity

  1. kapadia commented on Jul 2, 2014

    @kapadia
    Contributor

    I added a colorinterp method to the RasterReader and write_colorinterp to RasterUpdater. Tests are included, and I did a sanity check with gdalinfo :)

  2. kapadia commented on Jul 2, 2014

    @kapadia
    Contributor

    For the enums, grey or gray?

    Greyscale */                                      GCI_GrayIndex=1,
    
  3. sgillies commented on Jul 2, 2014

    @sgillies
    MemberAuthor

    👍 for both.

  4. sgillies commented on Jul 2, 2014

    @sgillies
    MemberAuthor

    Merged. There's a quirk which might be GDAL's:

    =================================== FAILURES ===================================
    _______________________________ test_colorinterp _______________________________
    
    tmpdir = local('/private/var/folders/zq/fv6jds3j4wb_0z0zh35xky_c0000gn/T/pytest-181/test_colorinterp0')
    
        def test_colorinterp(tmpdir):
    
            with rasterio.drivers():
    
                with rasterio.open('rasterio/tests/data/RGB.byte.tif') as src:
                    r, g, b = map(src.read_band, (1, 2, 3))
                    meta = src.meta
    
                    assert src.colorinterp(1) == ColorInterp.red
                    assert src.colorinterp(2) == ColorInterp.green
                    assert src.colorinterp(3) == ColorInterp.blue
    
                tiffname = str(tmpdir.join('foo.tif'))
    
                with rasterio.open(tiffname, 'w', **meta) as dst:
                    dst.write_band(1, r)
                    dst.write_band(2, g)
                    dst.write_band(3, b)
    
                    dst.write_colorinterp(1, ColorInterp.gray)
                    dst.write_colorinterp(2, ColorInterp.grey)
                    dst.write_colorinterp(3, ColorInterp.gray)
    
    >               assert dst.colorinterp(1) == ColorInterp.gray
    E               assert <ColorInterp.red: 3> == <ColorInterp.gray: 1>
    E                +  where <ColorInterp.red: 3> = <built-in method colorinterp of rasterio._io.RasterUpdater object at 0x1022d1c20>(1)
    E                +    where <built-in method colorinterp of rasterio._io.RasterUpdater object at 0x1022d1c20> = <open RasterUpdater name='/private/var/folders/zq/fv6jds3j4wb_0z0zh35xky_c0000gn/T/pytest-181/test_colorinterp0/foo.tif' mode='w'>.colorinterp
    E                +  and   <ColorInterp.gray: 1> = ColorInterp.gray
    

    It looks like GDAL might be overriding the color interpretaton we provide. I'll dig and see what's up.

  5. sgillies commented on Jul 2, 2014

    @sgillies
    MemberAuthor

    @kapadia check this out: https://trac.osgeo.org/gdal/ticket/4547

    SetColorInterpretation?() on the GTiff driver only really work on alpha bands. The color interpretation is dedecuded from the TIFF Photometric TAG, the presence of color table and the value of the ExtraSamples? tag.

    It looks like TIFF tags are the only way to update the color interpretation. I'm inclined to remove write_colorinterp() and document how to use the photometric creation option for the GTiff format: http://www.gdal.org/frmt_gtiff.html.

  6. added a commit that references this issue on Jul 2, 2014
  7. sgillies commented on Jul 2, 2014

    @sgillies
    MemberAuthor

    It looks like a photometric creation option is the only foolproof way to do it. Here's an example from the tests:

        with rasterio.drivers():
    
            with rasterio.open('rasterio/tests/data/RGB.byte.tif') as src:
                assert src.colorinterp(1) == ColorInterp.red
                assert src.colorinterp(2) == ColorInterp.green
                assert src.colorinterp(3) == ColorInterp.blue
    
            tiffname = str(tmpdir.join('foo.tif'))
    
            meta = src.meta
            meta['photometric'] = 'CMYK'
            meta['count'] = 4
            with rasterio.open(tiffname, 'w', **meta) as dst:
                assert dst.colorinterp(1) == ColorInterp.cyan
                assert dst.colorinterp(2) == ColorInterp.magenta
                assert dst.colorinterp(3) == ColorInterp.yellow
                assert dst.colorinterp(4) == ColorInterp.black
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions