[gdal-dev] SetColorInterpretation() for a GeoTiff using GDAL not working

Even Rouault even.rouault at mines-paris.org
Fri Nov 29 03:04:51 PST 2013


Selon philwilkes <phil.wilkes at rmit.edu.au>:

> I have also posted this on http://gis.stackexchange.com/ but had no joy so
> thought I'd try my luck here!
>
> I have a single band image which I would like to copy to a 3 bands of a new
> image and set the ColorInterpretation to red, green and blue respectively. I
> have tried two approaches, one using the GTiff driver, explained below, and
> the other using the VRT driver.
>
> My code is;
>
> ##############################################
>
> from osgeo import gdal
> import os
>
> IM = "path/to/image.tif" # single band image
>
> ### read image ###
> ds = gdal.Open(IM)
> X = ds.RasterXSize
> Y = ds.RasterYSize
> band = ds.GetRasterBand(1).ReadAsArray()
>
> ### write to 3 bands ###
> driver = gdal.GetDriverByName("GTiff")
> outPath = os.path.join(os.path.split(IM)[0], "test_image.tif")
> outIM = driver.Create(outPath, X, Y, 3, gdal.GDT_Int16)
> for i in range(1, 4):
>     outIM.GetRasterBand(i).SetRasterColorInterpretation(2 + i)
>     outIM.GetRasterBand(i).WriteArray(band)
>     print outIM.GetRasterBand(i).GetRasterColorInterpretation()
> outIM = None
>
> ##############################################
>
> The output is:
>
> 1
> 0
> 0
>
> I have also tried using the SetColorInterpretation().
>
> My question is, how do I set the ColorInterpretation metatag using the GTiff
> driver?

Phil,

I've reviewed quickly the code of the GeoTIFF driver and indeed there's some
inconsistency in round-tripping of SetColorInterpretation() /
GetColorInterpretation().

The main reason is that fundamentally color interpretation in tiff format is not
a per-band setting, but a per-dataset setting. You should try outIM =
driver.Create(outPath, X, Y, 3, gdal.GDT_Int16, options = [ 'PHOTOMETRIC=RGB' ])
and you should not need to call SetColorInterpretation()

In the case of TIFF, SetColorInterpretation() has influence, at the TIFF file
format, only when setting alpha on the 2nd band of a 2 band dataset (grey +
alpha) or the 4th band of a 4 band dataset (RGB + alpha). All other calls will
set value at the persistance auxiliary metadata system level (.aux.xml files).
But it would be desirable that SetColorInterpretation() and
GetColorInterpretation() correctly round-trip. Would you mind opening a ticket
in GDAL trac about that ?

Best regards,

Even




More information about the gdal-dev mailing list