[gdal-dev] Convert alpha to nodata?

Even Rouault even.rouault at spatialys.com
Tue Jan 23 18:27:19 PST 2024


Rob,

https://github.com/OSGeo/gdal/pull/9128 should do what you wish. As 
usual, the hardest part is the naming of options...

Even

Le 19/01/2024 à 18:32, Even Rouault via gdal-dev a écrit :
> Hi Rob,
>
> I can't think an easy way of doing what you want.
>
> Well, you can definitely do that using a VRT Python pixel function, 
> that would essentially do your --calc expression
>
> Cf 
> https://gdal.org/drivers/raster/vrt.html#using-derived-bands-with-pixel-functions-in-python
>
> The example of 
> https://gdal.org/drivers/raster/vrt.html#vrt-that-adds-2-or-more-rasters 
> is probably the closest starting point. (you can add <SourceBand> to 
> the <SimpleSource>'s to specify bands 1 and 4)
>
> A nice exercice for (Pythonist) contributors would be to add a -f VRT 
> capability to gdal_calc.py that would essentially automate the writing 
> of such VRT using Python pixel functions. Just saying...
>
> Even
>
> Le 19/01/2024 à 17:40, Robert Coup via gdal-dev a écrit :
>> Maybe it's just Friday, but I could use a pointer here :-)
>>
>> My input raster is a RGBA byte image, where the alpha channel value 
>> is 0 or 255 (ie: effectively a mask). The output is ideally a VRT 
>> file, RGB with Int16 data type, using nodata=-1.
>>
>> So I want to convert the alpha channel to a nodata value.
>>
>> The alpha/nodata/mask/band options to translate/warp haven't got me 
>> very far, neither has hand-crafting various UseMaskBand/MaskBand 
>> elements in a VRT.
>>
>> I can do it to concrete tif files with something like this, 
>> repeating for R,G,B and merge the output bands together, but it's a 
>> lot of processing.
>>
>>   gdal_calc.py -A src.tif -B src.tif \
>>     --outfile out_b1.tif --co TILED=YES --type=Int16 --NoDataValue=-1 \
>>     --A_band=1 --B_band=4 --calc="where(B >= 128, A, -1)"
>>
>> I feel like I'm probably missing a simpler path? (Which I will 
>> definitely add to an Examples section somewhere)
>>
>> Thanks,
>>
>> Rob :)
>>
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>
-- 
http://www.spatialys.com
My software is free, but my time generally not.



More information about the gdal-dev mailing list