Archive for the 'Uncategorized' Category

interpolating colors

I’m starting a little project: making a python function to apply a color scheme to a gray level image.

For that we need: to read the image (job done by gdal), read a color scheme, get image min an max excluding no data values, interpolate the color scheme, and last but not least, transform the single band gray level into an rgb image.

For today, let’s start with the color interpolation function. Color can be interpolated in rgb, hsv, etc. Let’s write something for rgb, easily portable to other color space.

The idea is to consider separately the 3 color axis and interpolate along them:

def interpolateColor(rgbMin, rgbMax, maxDepth, depth):
    if depth <= 0:
        return rgbMin
    if depth >= maxDepth:
        return rgbMax

    for iTriplet in range(3):
        rgb[iTriplet] = int(minVal + (maxVal-minVal)*(depth/float(maxDepth)))

    return rgb

def test():
    rgbMin=[128, 0 ,34]
    rgbMax=[255, 56, 0]

    for ii in range(niter):
       print ii, interpolateColor(rgbMin, rgbMax, niter, ii)

To test it:

import interpolateColor

You should get the 9 intermediate colors.

Some tips on HDF5 files

In general, my users ask me to export HDF formatted images into something directly usable with GIS desktop software (Geotiff, Erdas Imagine, etc.)

The problem with HDF is that it is not an image format but a data container format: it’s very general, can contain any type of object (variables, arrays, images…). The best way to handle this format is to write some lines of code to browse the file internal table storing the meta information.

From the command line, you can use gdalinfo to get some meta-information. The meta information can be more or less complex depending on what was stored in the HDF file.
Let’s consider an HDF5 file, which metainformation would be

Driver: HDF5/Hierarchical Data Format Release 5
Size is 512, 512
Coordinate System is `'
SUBDATASET_0_DESC=[1511x701] //FVC (8-bit integer)
SUBDATASET_1_DESC=[1511x701] //FVC_QF (8-bit character)
SUBDATASET_2_DESC=[1511x701] //FVC_err (8-bit integer)
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 512.0)
Upper Right ( 512.0, 0.0)
Lower Right ( 512.0, 512.0)
Center ( 256.0, 256.0)

In HDF you can store different types of data in the same file, the Size information for the file is not meaningful in this case (here it is written that Size is 512, 512, which is wrong since the actual size of the images is given on the lines with the SUBADATASET_0_DESC.

The image size given in the header is not meaninful: the images are 1511×701 lines, as indicated in the line SUBDATASET_0_DESC and not 512×512 in the header. The same SUBADATASET_0_DESC line gives you the file type.

The example above is about an HDF5 image, but FWTools can also handle HDF4 images.

Now, to export the image to something easier to handle, you must give the dataset name to gdal_translate, not the hdf5 file name:

gdal_translate -of gtiff HDF5:"HDF5_LSASAF_MSG_FVC_SAme_200806100000"://FVC fvc.tif

to export the data set named FVC into a single image.

Batch renaming files

Whether you are using linux (bash or shell) or cygwin, renaming a (large) set of files is really easy. Because renaming a file and changing its location is the same thing for linux and cygwin, you’ve got a large set of possibilities.

First example. Say you have 200 files in a directory that must receive a prefix like new_. Here is what you can write from the prompt.

for f in * ; do mv $f new_$f ; done

and ALL files in the current directory have their names starting with new_ now.
The star * tells to consider any file. For each file found, its name is stored in the variable f and the command mv $f new_$f is executed. The $f is replaced by each filename.
Moving the files to another place is trivial. Say you have a directory named source and another named target, you can:

cd source
for f in * ; do mv $f ../target/new_$f ; done
cd ..

(you can use cp instead of mv to copy the original files to another named file).

Second example. Now you want to change the file extension. It comes that bash lets you do some operations on the variables. For example, if f contains a string that ends with .img, say my_image.img, the instruction ${f%.img} would remove the extension .img from the end of the string (more manipulation in future posts). Renaming files ending with .img with .ida would be:

for f in *.img ; do mv $f ${f%.img}.ida ; done

More on files manipulation in future posts!