[gdal-dev] changing white pixels to transparent
Frank Warmerdam
warmerdam at pobox.com
Fri Feb 1 11:10:50 EST 2008
Andreas Neumann wrote:
> Hello!
>
> I have 1bit (b+w) input GeoTIFF files and want to change them to 8bit gray
> value pixels where the white bits are set to transparent. I had a look at
> the gdal_translate and GeoTIFF options but did not find a way to
> accomplish this task. Maybe I have overlooked some parameters/switches?
>
> If this can't be accomplished with gdal - can you recommend another tool,
> preferably a commandline tool?
Andreas,
One approach to this would be to use a VRT file as a mechanism to modify the
palette.
eg.
gdal_translate -of VRT fax2d.tif fax2d.vrt
Then edit fax2d.vrt which looks like this:
<VRTDataset rasterXSize="1728" rasterYSize="1082">
<Metadata>
<MDI key="TIFFTAG_SOFTWARE">fax2tiff</MDI>
<MDI key="TIFFTAG_XRESOLUTION">204</MDI>
<MDI key="TIFFTAG_YRESOLUTION">98</MDI>
<MDI key="TIFFTAG_RESOLUTIONUNIT">2 (pixels/inch)</MDI>
</Metadata>
<VRTRasterBand dataType="Byte" band="1">
<Metadata/>
<ColorInterp>Palette</ColorInterp>
<ColorTable>
<Entry c1="255" c2="255" c3="255" c4="255"/>
<Entry c1="0" c2="0" c3="0" c4="255"/>
</ColorTable>
<SimpleSource>
<SourceFilename
relativeToVRT="0">/gdal/root/usr2/data/geotiff/misc/fax2d.tif</SourceFilename>
<SourceBand>1</SourceBand>
<SrcRect xOff="0" yOff="0" xSize="1728" ySize="1082"/>
<DstRect xOff="0" yOff="0" xSize="1728" ySize="1082"/>
</SimpleSource>
</VRTRasterBand>
</VRTDataset>
Modify the color table entry for pixel value 0 (white) to have an
alpha value of zero:
<Entry c1="255" c2="255" c3="255" c4="0"/>
Then you can turn this VRT dataset back into a concrete file like:
gdal_translate -of PNG fax2d.vrt fax2d.png
Note that I didn't write it out as TIFF. TIFF files don't actually
have a mechanism for representing alpha (transparency) values in
color tables so our change would effectively be lost. PNG files;
however, do.
This may not be the most elegant approach but perhaps it will suit
your needs. The color table modification step could also be scripted
in python, awk or whatever if you want to fully automate the process.
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 | President OSGeo, http://osgeo.org
More information about the gdal-dev
mailing list