[gdal-dev] Giventiff with colortable, how do i assign one of the colors to be an alpha channel

Even Rouault even.rouault at mines-paris.org
Wed Jan 16 11:21:31 PST 2013


Le mercredi 16 janvier 2013 20:03:14, Isaac Gerg a écrit :
> Hi All,
> 
> I have a tiff in which each pixel is a single byte (0-255) which references
> a colortable.  Each element of the colortable has 4 values (RGBA).
> 
> I would like to convert my "black" color (entry 1 in the color table
> (0,0,0,255)) to be transparent (new colortable entry is (0,0,0,0))
> 
> Another way to say it is, how do I create a tiff with an indexed colormap
> but also have it contain an alpha channel?

TIFF color palette are only RGB, not RGBA. What you can do, however, is assign 
a nodata value (note this is a GDAL specific TIFF tag that will only be 
understood by GDAL based software)

gdal_translate src.tif src_with_nodata.tif -a_nodata 0

You can create a TIFF with an indexed colormap and an alpha channel with the 
following extra steps :

gdal_translate src_with_nodata.tif mask.tif -b mask
gdalbuildvrt tmp.vrt src.tif mask.tif -separate
gdal_translate src.tif src.vrt -of VRT

the with a text editor copy the lines that look line the following ones from 
src.vrt

    <ColorInterp>Palette</ColorInterp>
    <ColorTable>
      <Entry c1="112" c2="120" c3="56" c4="255" />
[...]
      <Entry c1="204" c2="208" c3="164" c4="255" />
    </ColorTable>

and paste them just below <VRTRasterBand dataType="Byte" band="1"> in tmp.vrt

Finally :

gdal_translate tmp.vrt pct_with_alpha.tif -co ALPHA=YES

That's it. But I'm not sure that this will be properly displayed by viewers.

A safest alternative would be to build a RGBA TIFF :

gdal_translate -expand rgb src.tif rgb.tif
gdal_translate -b 1 rgb.tif r.tif
gdal_translate -b 2 rgb.tif g.tif
gdal_translate -b 3 rgb.tif b.tif
gdalbuildvrt rgba.vrt r.tif g.tif b.tif mask.tif -separate
gdal_translate rgba.vrt rgba.tif  -co ALPHA=YES

> 
> Thanks in advance,
> Isaac


More information about the gdal-dev mailing list