[gdal-dev] More information to understand ColorTables in GeoTiffs
Robert Nix
robert at urban4m.com
Fri Mar 8 14:05:08 PST 2013
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?
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 ");
--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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20130308/5b1da38d/attachment.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/20130308/5b1da38d/attachment.bin>
More information about the gdal-dev
mailing list