[Gdal-dev] Creating GIFs, JPEGS, etc

Frank Warmerdam warmerdam at pobox.com
Wed Aug 25 09:11:56 EDT 2004

Gerald Brandt wrote:
> Hi,
> I'm succesfully loading DTED, USGS DEM and GTOPO30 DEM files.  I use
> these data files to get elevation information for specific latitudes and
> logitudes.
> What I would like to do is display an image (BMP, GIF, JPEG) of the
> elevation data in different colours.  I'm not quite sure on how to do
> this properly.  Is there an 'easy' way to do this with GDAL?


When producing 8bit (or 24bit) image files from elevation data, it is necessary
to scale the data to 8bit first.  From the commandline this can be done with
a command like:

   gdal_translate -of JPEG -ot Byte -scale W100N40.DEM out.jpg

You can look through gdal_translate to get an idea of how it accomplishes the

Applying a colortable is a bit more complex.  Note that BMP and GIF support
paletted images while JPEG only supports greyscale or 24bit RGB output.  So
the approach will eventually differ a bit for these formats.   Addressing the
case of formats that support palettes (color tables), you would need to create
a GDALColorTable and initialize the entries appropriately.  Then set it on
the target raster band with SetColorTable().

However, only a limited set of formats support dynamic setting of the colortable.
It appears BMP does but I don't think GIF does.

To produce JPEG (24bit) output, you will likely be best off creating another
format, like BMP, and then using the pct2rgb.py script or equivelent to
convert to 24bit.

A number of other options exist of course.  If the image you want to work
with isn't too large you could create an in-memory image of what you want and
then use GDALCreateCopy() to write that to the desired format.

All in all, these sort of transformations require a fair amount of work
compared to simple untransformed translations.

Best regards,
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

More information about the Gdal-dev mailing list