[gdal-dev] More information to understand ColorTables in GeoTiffs

Robert Nix robert at urban4m.com
Sun Mar 10 19:21:42 PDT 2013


Thanks Jan (and Frank),

Problem 1 was that i had overlooked that SetColorEntry returns void.
Problem 2 was that i expected i needed to pass a ColorEntry to CreateColorRamp instead of just passing tuples.
Problem 3 was that i was creating the color table as gdal.ColorTable(gdal.GCI_PaletteIndex) but apparently it must be just gdal.ColorTable(). 
This one was a little confusing since http://www.gdal.org/classGDALColorTable.html#a49b488f77a913fffbdffb5e4cb6b9226 states:

> "… the passed in entry must match the color interpretation of the table to which it is being assigned."

which i took to mean that i needed to create the color table the same as i set the raster band's color interpretation, i.e:

	band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)

Anyway, having those three problems all at the same time was confusing the issue.

So, this works:

	ds = driver.Create('ct.tif', 100, 100, 1, gdal.GDT_Byte)
	band = ds.GetRasterBand(1)
	band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)

	c = gdal.ColorTable()
	c.CreateColorRamp(0,(0,0,0),127,(255,0,0))
	c.CreateColorRamp(128,(0,0,0),255,(0,0,255))
	band.SetColorTable(c)

	band.WriteArray(m)

	ds = None

Here's another thread for reference:
http://lists.osgeo.org/pipermail/gdal-dev/2012-September/034142.html

Thanks,
--nix

On Mar 10, 2013, at 16:57, Jan Heckman <jan.heckman at gmail.com> wrote:

> Robert,
> Afaik, you need a three tuple for the color info. See e.g. http://osgeo-org.1560.n6.nabble.com/gdal-dev-tiff-with-colortable-loses-alpha-values-td3747611.html.
> It has a line giving an apparently working SetColorEntry() call without also using CreateColorRamp() which just complicates things here.
> For tuples in python, http://docs.python.org/release/1.5.1p1/tut/tuples.html gives a useful explanation.
> Hope this helps a bit,
> Jan
> 
> On Sat, Mar 9, 2013 at 4:52 AM, robert <robert at urban4m.com> wrote:
> Frank,
> 
> Thanks for the response.
> 
> I've tried variations of [] and (), with and without alpha (3 or 4 values), and with various values ranging from 0 to 255. They all have failed so it seems i'm doing something fundamentally wrong.
> 
> 
> Traceback (most recent call last):
>   File "makecolortable.py", line 13, in <module>
>     c.CreateColorRamp(1,n,65535,m)
>   File "/usr/lib/python2.7/dist-packages/osgeo/gdal.py", line 1117, in CreateColorRamp
>     return _gdal.ColorTable_CreateColorRamp(self, *args)
> TypeError: not a sequence
> 
> 
> Traceback (most recent call last):
>   File "makecolortable.py", line 11, in <module>
>     n = c.SetColorEntry(1,[0,0,0])
>   File "/usr/lib/python2.7/dist-packages/osgeo/gdal.py", line 1113, in SetColorEntry
>     return _gdal.ColorTable_SetColorEntry(self, *args)
> TypeError: Invalid values in ColorEntry sequence 
> 
> 
> 
> import numpy as np
> import gdal
> 
> driver = gdal.GetDriverByName('GTiff')
> dst_ds = driver.Create('testcolor.tif', 100, 100, 1, gdal.GDT_UInt16)
> 
> band = dst_ds.GetRasterBand(1)
> band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)
> 
> c = gdal.ColorTable(gdal.GCI_PaletteIndex)
> n = c.SetColorEntry(1,[0,0,0])
> m = c.SetColorEntry(65535,[0,0,0])
> c.CreateColorRamp(1,n,65535,m)
> band.SetColorTable(c)
> 
> --nix
> 
> On 2013/03/08, at 19:09, Frank Warmerdam <warmerdam at pobox.com> wrote:
> 
>> On Fri, Mar 8, 2013 at 2:05 PM, Robert Nix <robert at urban4m.com> wrote:
>>> Let me adjust my question. I changed the color-table code to:
>>> 
>>> c = gdal.ColorTable(gdal.GCI_PaletteIndex)
>>> n = c.SetColorEntry(1,[255,128,0,0])
>>> m = c.SetColorEntry(65535,[0,128,255,255])
>>> c.CreateColorRamp(1,n,65535,m)
>>> band.SetColorTable(c)
>>> 
>>> 
>>> I discovered that the 64K colors are due to the UInt16 (tested GDT_Byte and
>>> got 255) so i realized that maybe i need to create a ramp?
>> 
>> Robert,
>> 
>> I will note that the TIFF format does not support "short colormaps".
>> So if the type of the pixel is 8bit byte then the color table will be
>> exactly 256 entries even if you only put in one color.  Frr UInt16 it
>> will be 65536.  Most other colormap supporting formats do not impose
>> this.
>> 
>>> But the above code fails with:
>>> 
>>> Traceback (most recent call last):
>>>  File "makecolortable.py", line 14, in <module>
>>>    n = c.SetColorEntry(1,[255,128,0,0])
>>>  File "/usr/lib/python2.7/dist-packages/osgeo/gdal.py", line 1113, in
>>> SetColorEntry
>>>    return _gdal.ColorTable_SetColorEntry(self, *args)
>>> TypeError: Invalid values in ColorEntry sequence
>>> 
>>> 
>>> That sequence appears correct as all four values meet the short requirement,
>>> (i think this is the code that produces the error):
>>> 
>>> if ( !PyArg_ParseTuple( $input,"hhh|h", &ce.c1, &ce.c2, &ce.c3, &ce.c4 ) ) {
>>> 
>>>  PyErr_SetString(PyExc_TypeError, "Invalid values in ColorEntry sequence
>>> ");
>> 
>> 
>> I think the problem is that you switched from tuples to lists (ie.
>> from (r,g,b,a) to [r,g,b,a]).
>> 
>> Also, FYI, TIFF does not support alpha in colormaps.
>> 
>> Best regards,
>> Frank
>> 
>>> --nix
>>> 
>>> On Mar 8, 2013, at 16:35, Robert Nix <robert at urban4m.com> wrote:
>>> 
>>> Hi,
>>> 
>>> I'm trying to add a color table to a 1-band UInt16 GeoTiff with Python as
>>> follows:
>>> 
>>> import numpy as np
>>> import gdal
>>> 
>>> driver = gdal.GetDriverByName('GTiff')
>>> dst_ds = driver.Create('testcolor.tif', 100, 100, 1, gdal.GDT_UInt16)
>>> 
>>> band = dst_ds.GetRasterBand(1)
>>> band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)
>>> 
>>> c = gdal.ColorTable(gdal.GCI_PaletteIndex)
>>> c.SetColorEntry(1,(255,127,0,63))
>>> band.SetColorTable(c)
>>> 
>>> a = np.random.randint(1,10,size=(100,100))
>>> band.WriteArray(a)
>>> 
>>> dst_ds = None
>>> 
>>> 
>>> The problem is that when i run gdalinfo on the testcolor.tif, it lists 65535
>>> color table entries:
>>> 
>>> Driver: GTiff/GeoTIFF
>>> Files: testcolor.tif
>>>       testcolor.tif.aux.xml
>>> Size is 100, 100
>>> Coordinate System is `'
>>> Image Structure Metadata:
>>>  INTERLEAVE=BAND
>>> Corner Coordinates:
>>> Upper Left  (    0.0,    0.0)
>>> Lower Left  (    0.0,  100.0)
>>> Upper Right (  100.0,    0.0)
>>> Lower Right (  100.0,  100.0)
>>> Center      (   50.0,   50.0)
>>> Band 1 Block=100x40 Type=UInt16, ColorInterp=Palette
>>>  Color Table (RGB with 65536 entries)
>>>    0: 1,0,0,255
>>>    1: 1,0,0,255
>>>    2: 0,0,0,255
>>>    3: 0,0,0,255
>>> ...
>>>  65533: 0,0,0,255
>>>  65534: 0,0,0,255
>>>  65535: 0,0,0,255
>>> 
>>> 
>>> What's going on … besides the fact that maybe i'm not understanding
>>> color-tables? If it's clear i don't understand, is there a good resource for
>>> understanding color-tables? Or must I go learn the source code (which i
>>> really don't have to do)?
>>> 
>>> Thanks
>>> --nix
>>> 
>>> 
>>> 
>>> _______________________________________________
>>> gdal-dev mailing list
>>> gdal-dev at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>> 
>> 
>> 
>> -- 
>> ---------------------------------------+--------------------------------------
>> I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
>> light and sound - activate the windows | http://pobox.com/~warmerdam
>> and watch the world go round - Rush    | Geospatial Software Developer
> 
> 
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20130310/09fc996c/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1437 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20130310/09fc996c/attachment-0001.bin>


More information about the gdal-dev mailing list