[gdal-dev] changing white pixels to transparent

Andreas Neumann a.neumann at carto.net
Tue Feb 5 12:08:32 EST 2008


Hi Frank and others,

thanks for answering. I can confirm that the approach through the vrt file
and editing the color-table works fine.

I wonder if the TIFF format really doesn't support transparency. I was
able to save a transparent tiff file in GIMP and it loaded fine next time,
with transparency preserved. Is this a limitation of gdal or associated
libraries?

When converting to PNG: how does it handle compression? I did not see any
documentation on how to control the compression. Also, can I convert to a
grayscale PNG from my original b/w tif? Are there any additional co
options for the PNG format?

Are there other formats beside PNG that support/preserve transparency?

Thank you for getting back on this issue,
Andreas

> 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
>
>


-- 
Andreas Neumann
Böschacherstrasse 6, CH-8624 Grüt/Gossau, Switzerland
Email: a.neumann at carto.net, Web:
* http://www.carto.net/ (Carto and SVG resources)
* http://www.carto.net/neumann/ (personal page)
* http://www.svgopen.org/ (SVG Open Conference)
* http://www.geofoto.ch/ (Georeferenced Photos of Switzerland)



More information about the gdal-dev mailing list