We have a specific process which generate an interpolated single band 32-bit Float Raster in Erdas Imagine (.IMG) format. This process generates an image every hour.

We wish to apply a specific stretched renderer, which stretches from Green to Red, to the single band raster and then save it as a JPEG file.

I have found that it can be done through the UI in ArcMap by applying the .lyr file, and then exporting to JPEG 2000. The Jpeg 2000 is converted to the regular JPEG image by python.

We now want to automate this process and run it using some open source or free tool.

Is it possible to do something like this in GDAL or some other command line tool?

  • 1
    I believe you should be able to do something similar to this with gdaldem (color-relief mode), but you would have to know the min and max values before hand and build the color_text_file with at least two colors, possibly three: gdaldem. – Evil Genius Apr 10 '14 at 15:18

Evil Genius seems to have a good idea, but actually the color-relief file can be set using percentages as well as min and max. So you could write the file using something like:

0%    green
100%  red

So the command would be something like:

gdaldem color-relief inputfile.img colorfile.txt output.jpg -of "JPEG2000"
  • Does this work for you? It was the first thing I tried but but the output image was either all red or all green depending on the ordering of the items in the text file. – nhopton Apr 11 '14 at 7:42
  • This works for me, except for one small thing. My IMG is 250 px*250 px. I want to make a Jpeg of twice that dimensions, i.e. 500px*500px. Can you tell me how I could do this? – Devdatta Tengshe Apr 11 '14 at 10:49
  • use gdalwarp first to change the pixel size. – radouxju Apr 11 '14 at 11:04
  • You can also do it with just gdal_translate using the -outsize parameter: gdal_translate – Evil Genius Apr 11 '14 at 11:31
  • Thanks for your answer. I have now posted a new question about writing the color configuration file: gis.stackexchange.com/questions/92779/… – Devdatta Tengshe Apr 11 '14 at 11:57

You could use the R "raster", "rasterVis" and "ggplot2" packages to automate this quite nicely. You have considerable control of very simple (just the raster) or customized plots using the R low-level plotting engine or higher level plotting like ggplot2. You can also easily call other plotting devices to output other formats including: tiff, bmp, png or pdf. I have used this exact approach in concert with the animation package to create raster, html and giff, animations.

Here is an example using a raster from the raster package. Note; I commented out the jepg device call and the dev.off() so the result will plot to a window device.


# Add example raster
r <- raster(system.file("external/test.grd", package="raster"))
  names(r) <- c('meuse')

# Call ggplot through gplot interface and then call jpeg graphic device
#jpeg(paste(dir, "RasterPlot.jpg", sep="/"), width=480, height=480, quality=100) 
    rplot <- ( gplot(r) + geom_tile(aes(fill = value)) +
              facet_wrap(~ variable) +
              scale_fill_gradient(low='green', high='red') +
              coord_equal() )

You can use R's looping structure to automate the process.


# Set raster and output directories
ImageDir = "C:/Images" 
OutDir = "C:/Images/jpegs"

# Make a raster stack of all img files in ImageDir
rstack <- stack( list.files(ImageDir, pattern="img$", full.names=TRUE)  )

# For loop to create jpeg for each raster. Name is same as raster
for(i in 1:nlayers(rstack)) {
  jpeg(paste(OutDir, paste( names(rstack)[i], "jpg", sep="."), sep="/"), 
             width=480, height=480, quality=100) 
      rplot <- ( gplot(rstack[[i]]) + geom_tile(aes(fill=value)) +
                facet_wrap(~ variable) +
                scale_fill_gradient(low='green', high='red') +
                coord_equal() )
                print( rplot )

You could use the OTB applications :

first rescale your data in 8 bit

otbcli_Rescale -in input_image.tif -out intermediate_image.tif uchar -outmin 0 -outmax 255

then apply a colormap

otbcli_ColorMapping -in intermediate_image.tif -method custom -method.custom.lut yourLUT.txt -out output_image.jpg

your LUT should go from

0 0 255 0


255 255 0 0

you can build it in Python with something like

for i in range(256):
    colors.append( str(i) + " " + str(i) + " " + str(255-i) + " 0" )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.