<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Maybe someone can just show me a working python example of how to create a color table in a tiff using gdal?</div><div><br></div><div>Here's what i've tried. It seems all the "Invalid value" errors are the closest to success.</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div style="text-align: left;"><div><font face="Courier New"><br></font></div><div><div><font face="Courier New">c1 = c.SetColorEntry(1,"1,2")</font></div><div><font face="Courier New">TypeError: Invalid values in ColorEntry sequence </font></div><div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,"1,2,3")</font></div><div><font face="Courier New">TypeError: Invalid values in ColorEntry sequence </font></div><div><font face="Courier New"><br></font></div></div><div><font face="Courier New">c1 = c.SetColorEntry(1,[255,128,0,0])</font></div><div><font face="Courier New">TypeError: Invalid values in ColorEntry sequence </font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,[1,1,1,1])</font></div><div><font face="Courier New">TypeError: Invalid values in ColorEntry sequence </font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">from array import array</font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,array("h",[255,128,0,0]))</font></div><div><font face="Courier New">TypeError: Invalid values in ColorEntry sequence </font></div></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,{"c1":255,"c2":128,"c3":0,"c4":0})</font></div><div><div><font face="Courier New">TypeError: not a sequence</font></div></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,(255,128,0,0))</font></div><div><div><font face="Courier New">TypeError: not a sequence</font></div></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,"255,128,0")</font></div><div><div><font face="Courier New">TypeError: ColorEntry sequence too long</font></div></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,"1")</font></div><div><div><font face="Courier New">TypeError: ColorEntry sequence too short</font></div></div><div><font face="Courier New"><br></font></div><div><span style="font-family: 'Courier New'; ">c1 = c.SetColorEntry(1,1,1)</span></div><div><div><font face="Courier New">TypeError: ColorTable_SetColorEntry() takes exactly 3 arguments (4 given)</font></div></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,1)</font></div><div><div><font face="Courier New">TypeError: not a sequence</font></div></div><div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c1 = c.SetColorEntry(1,struct.pack("hhh",1,1,1))</font></div><div><font face="Courier New">TypeError: ColorEntry sequence too long</font></div></div><div><div><br></div></div><div><br></div></div></blockquote><div><br></div><div>What is GDALColorEntry expecting and <i>how to produce this from python</i> but, maybe more importantly, <b>why isn't it obvious</b>? It seems like most of the above should just work.</div><div><br></div><div>According to the <span style="font-family: 'Courier New'; ">GDALColorEntry</span> code below, it myst be a tuple of 3 or 4 short ints. </div><div><br></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font face="Courier New">%typemap(in) GDALColorEntry* (GDALColorEntry ce)</font></div><div><font face="Courier New">{</font></div><div><font face="Courier New">  /* %typemap(in) GDALColorEntry* */</font></div><div><font face="Courier New">   ce.c4 = 255;</font></div><div><font face="Courier New">  if (! PySequence_Check($input) ) {</font></div><div><font face="Courier New" color="#ff260e">    PyErr_SetString(PyExc_TypeError, "<b>not a sequence</b>");</font></div><div><font face="Courier New">    SWIG_fail;</font></div><div><font face="Courier New">  }</font></div><div><font face="Courier New">   int size = PySequence_Size($input);</font></div><div><font face="Courier New">   if ( size > 4 ) {</font></div><div><font face="Courier New" color="#ff260e">     PyErr_SetString(PyExc_TypeError, "<b>ColorEntry sequence too long</b>");</font></div><div><font face="Courier New">     SWIG_fail;</font></div><div><font face="Courier New">   }</font></div><div><font face="Courier New">   if ( size < 3 ) {</font></div><div><font face="Courier New" color="#ff260e">     PyErr_SetString(PyExc_TypeError, "<b>ColorEntry sequence too short</b>");</font></div><div><font face="Courier New">     SWIG_fail;</font></div><div><font face="Courier New">   }</font></div><div><font face="Courier New" color="#0b33ff">   if ( !PyArg_ParseTuple( $input,"<u><b>hhh|h</b></u>", &ce.c1, &ce.c2, &ce.c3, &ce.c4 ) ) {</font></div><div><font face="Courier New" color="#ff260e">     PyErr_SetString(PyExc_TypeError, "<b>Invalid values in ColorEntry sequence </b>");</font></div><div><font face="Courier New">     SWIG_fail;</font></div><div><font face="Courier New">   }</font></div><div><font face="Courier New">   $1 = &ce;</font></div><div><font face="Courier New">}</font></div></blockquote><div><br></div><div><br></div><div>
--nix

</div>
<br><div><div>On Mar 8, 2013, at 22:52, robert <<a href="mailto:robert@urban4m.com">robert@urban4m.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><meta http-equiv="Content-Type" content="text/html charset=windows-1252"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Frank,</div><div><br></div><div>Thanks for the response.</div><div><br></div><div>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.</div><div><br></div><div><font face="Courier New"><br></font></div><div><div><font face="Courier New">Traceback (most recent call last):</font></div><div><font face="Courier New">  File "makecolortable.py", line 13, in <module></font></div><div><font face="Courier New">    c.CreateColorRamp(1,n,65535,m)</font></div><div><font face="Courier New">  File "/usr/lib/python2.7/dist-packages/osgeo/gdal.py", line 1117, in CreateColorRamp</font></div><div><font face="Courier New">    return _gdal.ColorTable_CreateColorRamp(self, *args)</font></div><div><font face="Courier New">TypeError: not a sequence</font></div></div><div><font face="Courier New"><br></font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">Traceback (most recent call last):</font></div><div><font face="Courier New">  File "makecolortable.py", line 11, in <module></font></div><div><font face="Courier New">    n = c.SetColorEntry(1,[0,0,0])</font></div><div><font face="Courier New">  File "/usr/lib/python2.7/dist-packages/osgeo/gdal.py", line 1113, in SetColorEntry</font></div><div><font face="Courier New">    return _gdal.ColorTable_SetColorEntry(self, *args)</font></div><div><font face="Courier New">TypeError: Invalid values in ColorEntry sequence </font></div><div><br></div><div><br></div><div><br></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><font face="Courier New">import numpy as np</font></div><div><font face="Courier New">import gdal</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">driver = gdal.GetDriverByName('GTiff')</font></div><div><font face="Courier New">dst_ds = driver.Create('testcolor.tif', 100, 100, 1, gdal.GDT_UInt16)</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">band = dst_ds.GetRasterBand(1)</font></div><div><font face="Courier New">band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">c = gdal.ColorTable(gdal.GCI_PaletteIndex)</font></div><div><font face="Courier New">n = c.SetColorEntry(1,[0,0,0])</font></div><div><font face="Courier New">m = c.SetColorEntry(65535,[0,0,0])</font></div><div><font face="Courier New">c.CreateColorRamp(1,n,65535,m)</font></div><div><font face="Courier New">band.SetColorTable(c)</font></div></blockquote><div><br></div><div>
--nix

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