[Gdal-dev] gdalwarp nodata

Frank Warmerdam warmerdam at pobox.com
Sat Nov 13 14:17:01 EST 2004


Frank Warmerdam wrote:
> OK I have attached a little script that takes in an RGB file, and outputs
> an RGBA file with alpha set to transparent(0) where the condition is 
> met, and
> to opaque(255) where it isn't set.
> 

Sorry .. this time I really attached it.


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

-------------- next part --------------
#!/usr/bin/env python

import gdal
import sys
import Numeric

try:
    src_file = sys.argv[1]
    dst_file = sys.argv[2]
except:
    print 'Usage: rgbamask.py <srcfile> <dstfile>'
    sys.exit(1)


# Open input file.

src_ds = gdal.Open( src_file )

# Create output file.

driver = gdal.GetDriverByName( 'GTiff' )
dst_ds = driver.Create( dst_file, src_ds.RasterXSize, src_ds.RasterYSize, 4 )

dst_ds.SetProjection( src_ds.GetProjection() )
dst_ds.SetGeoTransform( src_ds.GetGeoTransform() )

# ----------------------------------------------------------------------------
# Do the processing one scanline at a time. 

gdal.TermProgress( 0.0 )
for iY in range(src_ds.RasterYSize):
    src_red = src_ds.GetRasterBand(1).ReadAsArray(0,iY,src_ds.RasterXSize,1)
    src_green = src_ds.GetRasterBand(2).ReadAsArray(0,iY,src_ds.RasterXSize,1)
    src_blue = src_ds.GetRasterBand(3).ReadAsArray(0,iY,src_ds.RasterXSize,1)

    # Set alpha to transparent (0) where our condition is met, otherwise,
    # set it to 255.
    
    alpha = Numeric.where(src_red==255 and src_green==255 and src_blue==255,
                          0,255)

    dst_ds.GetRasterBand(1).WriteArray(src_red,0,iY)
    dst_ds.GetRasterBand(2).WriteArray(src_green,0,iY)
    dst_ds.GetRasterBand(3).WriteArray(src_blue,0,iY)
    dst_ds.GetRasterBand(4).WriteArray(alpha,0,iY)

    gdal.TermProgress( (iY+1.0) / src_ds.RasterYSize )
    
dst_ds = None







More information about the Gdal-dev mailing list