<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; ">Thanks Jan (and Frank),<div><br></div><div>Problem 1 was that i had overlooked that SetColorEntry returns void.</div><div>Problem 2 was that i expected i needed to pass a ColorEntry to CreateColorRamp instead of just passing tuples.</div><div>Problem 3 was that i was creating the color table as <font face="Courier New">gdal.ColorTable(gdal.GCI_PaletteIndex)</font> but apparently it must be just <font face="Courier New">gdal.ColorTable()</font>. </div><div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>This one was a little confusing since <a href="http://www.gdal.org/classGDALColorTable.html#a49b488f77a913fffbdffb5e4cb6b9226">http://www.gdal.org/classGDALColorTable.html#a49b488f77a913fffbdffb5e4cb6b9226</a> states:</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><blockquote type="cite">"… the passed in entry must match the color interpretation of the table to which it is being assigned."</blockquote><br></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;">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:</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><span class="Apple-tab-span" style="white-space:pre"> </span><font face="Courier New">band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)</font></div></blockquote><div><div><br></div></div><div>Anyway, having those three problems all at the same time was confusing the issue.</div><div><br></div><div>So, this works:</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ds = driver.Create('ct.tif', 100, 100, 1, gdal.GDT_Byte)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>band = ds.GetRasterBand(1)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>c = gdal.ColorTable()</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>c.CreateColorRamp(0,(0,0,0),127,(255,0,0))</div><div><span class="Apple-tab-span" style="white-space: pre; "> </span>c.CreateColorRamp(128,(0,0,0),255,(0,0,255))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>band.SetColorTable(c)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>band.WriteArray(m)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ds = None</div></div><div><br></div><div>Here's another thread for reference:</div><div><a href="http://lists.osgeo.org/pipermail/gdal-dev/2012-September/034142.html">http://lists.osgeo.org/pipermail/gdal-dev/2012-September/034142.html</a></div><div><br></div><div>Thanks,<br><div>
--nix
</div>
<br><div><div>On Mar 10, 2013, at 16:57, Jan Heckman <<a href="mailto:jan.heckman@gmail.com">jan.heckman@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Robert,</div>Afaik, you need a three tuple for the color info. See e.g. <a href="http://osgeo-org.1560.n6.nabble.com/gdal-dev-tiff-with-colortable-loses-alpha-values-td3747611.html">http://osgeo-org.1560.n6.nabble.com/gdal-dev-tiff-with-colortable-loses-alpha-values-td3747611.html</a>.<div>
It has a line giving an apparently working <span style="font-family:'Courier New'">SetColorEntry() call without also using </span><span style="font-family:'Courier New'">CreateColorRamp() which just complicates things here.</span><br>
<div>For tuples in python, <a href="http://docs.python.org/release/1.5.1p1/tut/tuples.html">http://docs.python.org/release/1.5.1p1/tut/tuples.html</a> gives a useful explanation.</div><div>Hope this helps a bit,</div><div>
Jan</div><div><br><div class="gmail_quote">On Sat, Mar 9, 2013 at 4:52 AM, robert <span dir="ltr"><<a href="mailto:robert@urban4m.com" target="_blank">robert@urban4m.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word"><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 class="im"><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 class="im"><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><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 class="im"><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></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="im"><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><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><span class="HOEnZb"><font color="#888888"><div><br></div><div>
--nix
</div></font></span><div><div class="h5">
<br><div><div>On 2013/03/08, at 19:09, Frank Warmerdam <<a href="mailto:warmerdam@pobox.com" target="_blank">warmerdam@pobox.com</a>> wrote:</div><br><blockquote type="cite">On Fri, Mar 8, 2013 at 2:05 PM, Robert Nix <<a href="mailto:robert@urban4m.com" target="_blank">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" target="_blank">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" target="_blank">gdal-dev@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/gdal-dev" target="_blank">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" target="_blank">warmerdam@pobox.com</a><br>light and sound - activate the windows | <a href="http://pobox.com/~warmerdam" target="_blank">http://pobox.com/~warmerdam</a><br>
and watch the world go round - Rush | Geospatial Software Developer<br></blockquote></div><br></div></div></div><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" target="_blank">http://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br></blockquote></div><br></div></div>
</blockquote></div><br></div></body></html>