[Gdal-dev] 1-band TIFF FILE color ramp NEEDED

alvarez00 oalvarez00 at gmail.com
Sat Apr 11 20:05:12 EDT 2009


Hi Roger,

I'm trying to run the python code you suggested but I keep getting this error :
Traceback (most recent call last):
  File "discreet_gray2color.py", line 14, in <module>
    gdal.TermProgress = gdal.TermProgress_nocb
AttributeError: 'module' object has no attribute 'TermProgress_nocb' "

I don't know if it's the version of gdal I'm using or something else. (i'm using gdal 1.4.1.0)

thanks,




Here's another way you can do it, a Python script that will create discreet
color classes from a grayscale image.  It uses a function named MakeColor
which is currently set to work on Z values between 0 and 255.  You can test
it by converting your file into an 8-bit version (gdal_translate -scale -ot
Byte <infile> <outfile> ), and then running the tool on it.  Once you see
how it works, it should be pretty simple to modify the function to deal with
the actual ranges in your data.

Roger
--

On Thu, Apr 9, 2009 at 4:24 PM, alvarez00 <oalvarez00 at gmail.com> wrote:

>
> I'm trying to make a color ramp for a 1-band tiff file or a jpeg. I have a
> 1-band tiff file and I converted that tiff file into VRT. I was reading some
> of the posts and some suggest to modify the VRT file and add a ColorTable
> tag but that doesn't work. My objective is to show the tiff(or)jpeg on
> google earth. I'm running version 1.4.1.0 of gdal. Below is the VRT file
> without any modifications. What I modify was the
> '<ColorInterp>Gray</ColorInterp>' to '<ColorInterp>Palette</ColorInterp>'
> and added a ColorTable.
>
> VRTDataset rasterXSize="1200" rasterYSize="1096">
>  <SRS>GEOGCS[&quot;NAD83&quot;,DATUM[&quot;North_American_Datum_1983&quot;,SPHEROID[&quot;GRS
> 1980&quot;,6378137,298.2572221010002,AUTHORITY[&quot;EPSG&quot;,&quot;7019&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;6269&quot;]],PRIMEM[&quot;Greenwich&quot;,0],UNIT[&quot;degree&quot;,0.0174532925199433],AUTHORITY[&quot;EPSG&quot;,&quot;4269&quot;]]</SRS>
>  <GeoTransform> -1.2459079918952935e+02,  8.9964705882352915e-03,
>  0.0000000000000000e+00,  4.2216820949146452e+01,  0.0000000000000000e+00,
> -8.9964705882352915e-03</GeoTransform>
>  <Metadata>
>    <MDI key="AREA_OR_POINT">Area</MDI>
>  </Metadata>
>  <VRTRasterBand dataType="Float32" band="1">
>    <Metadata/>
>    <NoDataValue>-3.40282346638529E+38</NoDataValue>
>    <ColorInterp>Gray</ColorInterp>
>    <SimpleSource>
>      <SourceFilename
> relativeToVRT="1">GoesWest1V1561915.tif</SourceFilename>
>      <SourceBand>1</SourceBand>
>      <SrcRect xOff="0" yOff="0" xSize="1200" ySize="1096"/>
>      <DstRect xOff="0" yOff="0" xSize="1200" ySize="1096"/>
>    </SimpleSource>
>  </VRTRasterBand>
> </VRTDataset>
>
> --
> View this message in context:
> http://n2.nabble.com/1-band-TIFF-FILE-color-ramp-NEEDED-tp2613963p2613963.html
> Sent from the GDAL - Dev mailing list archive at Nabble.com.
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>

#! /usr/bin/env python

# Create a colored image based on LUT in MakeColor function

# Author: Roger Andre, October 2008

# Usage: discreet_gray2color.py <infile> <outfile>

from osgeo import gdal
import sys
import numpy
import os.path
gdal.TermProgress = gdal.TermProgress_nocb

src_file = sys.argv[1]
dst_file = sys.argv[2]
out_bands = 3

def MakeColor(z_value):
  '''LUT for color ramp. Keys are pixel values, hash values are RGB triplets.'''
  color_dict = {
    0:[102,0,255],
   25:[20,82,255],
   50:[0,194,224],
   75:[0,255,122],
  100:[41,255,0],
  125:[204,255,0],
  150:[245,194,0],
  175:[224,118,0],
  200:[168,46,0],
  225:[105,0,0],
  250:[64,0,0],
  255:[0,0,0]}

  key_list = color_dict.keys()
  key_list.sort()
  while len(key_list) > 0:
    last_val = key_list[-1]
    if z_value >= last_val:
      return color_dict[last_val]
    else:
      key_list.remove(last_val)

# Print some info
print "Creating %s" % (dst_file)

# Open source file
src_ds = gdal.Open( src_file )
src_band = src_ds.GetRasterBand(1)

# create destination file
dst_driver = gdal.GetDriverByName('GTiff')
dst_ds = dst_driver.Create(dst_file, src_ds.RasterXSize, src_ds.RasterYSize, out_bands, gdal.GDT_Byte) 

# create output bands
band1 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize])
band2 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize])
band3 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize])

# set the projection and georeferencing info
dst_ds.SetProjection( src_ds.GetProjection() )
dst_ds.SetGeoTransform( src_ds.GetGeoTransform() )

# read the source file
gdal.TermProgress( 0.0 )
for iY in range(src_ds.RasterYSize):
  src_data = src_band.ReadAsArray(0,iY,src_ds.RasterXSize,1)
  col_values = src_data[0] # array of z_values, one per row in source data
  for iX in range(src_ds.RasterXSize):
    z_value = col_values[iX]
    # print z_value # uncomment to see what value breaks color ramp
    [R,G,B] = MakeColor(z_value)
    band1[iY][iX] = R
    band2[iY][iX] = G
    band3[iY][iX] = B
  gdal.TermProgress( (iY+1.0) / src_ds.RasterYSize )

# write each band out
dst_ds.GetRasterBand(1).WriteArray(band1)
dst_ds.GetRasterBand(2).WriteArray(band2)
dst_ds.GetRasterBand(3).WriteArray(band3)

dst_ds = None

_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


-- 
View this message in context: http://n2.nabble.com/1-band-TIFF-FILE-color-ramp-NEEDED-tp2613963p2622398.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.



More information about the gdal-dev mailing list