[Gdal-dev] Creation of geotiff files with gdalwarp that have a transparent background

Frank Warmerdam warmerdam at pobox.com
Mon Mar 1 16:51:06 EST 2004


Eric Sokolowsky wrote:
> I have been experimenting with the gdal library, specifically the
> gdalwarp tool included with the library.  It is very useful, and I have
> been able to transform images to a geographic projection.  Is there is a
> way to specify a default background color?  Specifically, I wish to
> create a geotiff file where the areas not covered by the original source
> file are transparent.  Is this possible, or do I need to start hacking
> gdal?  Would this change be generally useful to others?
> 

Eric,

The default output value of a raster can be set with the INIT_DEST
warp options.

eg.

gdalwarp in.tif out.tif -t_srs WGS84 -wo INIT_DEST=255

The arguments can be a list of values if you want to apply different
values to different output bands.  The default is to set all output bands
to zero.

One issue you face is how to make something transparent in
TIFF format.  TIFF supports transparency if you use RGBA format.  That
is four bands with red, green, blue and alpha (transparency).  But if
your inputs is just RGB there is no obvious way to force gdalwarp to
produce an RGBA output file, and to treat the alpha band as a proper
transparency overlay.

The underlying Warp API does contain mechanisms whereby an application
might be able to maintain an alpha band properly, but the gdalwarp
program has no mechanism to take advantage of it.  Furthermore, this
hasn't really be used in the warp api so there may be issues I didn't
anticipate.

Currently, the gdalwarp assumption is that nodata pixel values will be
used to mark unset areas.  So that might involve picking a "background"
value, like 0, to mark all unset areas.  INIT_DEST could be used to
pre-apply this to the output file if a non-zero value were used.  Then
the warper would replace all pixels for which a value exists.   No mechanism
exists to ensure that the nodata value doesn't existing in the valid image
data though.

I think it would make sense to modify gdalwarp to explicitly support
transparency layers.  Some common forms I could imagine are:

  o An extra output band used to hold transparency.  Normally this would
    be assigned 0 to unset pixels and 255 for set pixels.
  o A seperate validity file.

Both mechanisms should also be supported on input, in addition to the
current -srcnodata switch.

Implementing these capabilities would be a fair amount of work, likely at
least a day.  I don't forsee getting to it in the near future, but if you
have a pressing need I might be able to do a quick effort sufficient to
your needs.

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