[gdal-dev] Need help generating output with alpha channels using Python API

Michal Migurski mike at stamen.com
Fri Mar 18 01:41:26 EDT 2011


On Mar 16, 2011, at 9:58 AM, Frank Warmerdam wrote:

> On 11-03-14 12:50 PM, Michal Migurski wrote:
>> Hello,
>> 
>> I'm having some difficulties understanding how to get GDAL to generate
>> images with usable alpha channels. I have 3-channel RGB input JPEG image,
>> delivered to GDAL as a VRT with a projection and ground control points,
>> which I'm warping to an output that's no longer rectangular, leaving
>> background areas exposed. Here is an example output:
>> 
>> http://things.teczno.com/gnomotile.png
>> 
>> The black parts are intended to be transparent, but I haven't been able to
>> understand how to make that work. Here's the relevant part of my Python
>> code:
>> 
>> http://dpaste.com/hold/500308/
>> 
>> I've tried to switch to a four channel output which gets me what I think are
>> CMYK channels. I've tried to use SetNoDataValue on the destination bands to
>> make the background purple so it can be easily knocked out. I've tried to
>> create the GTiff output dataset using the ALPHA=YES creation option, but it
>> seemingly doesn't make a difference. None of these ideas has worked - does
>> anyone have any ideas on how the Python API can be used to create
>> transparent output?
> 
> Mike,
> 
> I would have thought preinitializing the destination dataset to a marker
> value should have done the trick.  I'm not sure what you mean by using
> SetNoDatavalue().  Are you aware that this just records metadata with the
> band indicating what the nodata value should be?

I figured that out after a while, but was casting about for a solution and trying anything that seemed potentially useful.


> Normally I would suggest adding an alpha band on the output and ensuring it
> is used.  But I see GDALReprojectImage() does not make it easy to do this
> particularly the way it is bound in Python which does not allow one to
> pass in the warp options structure.

What I ended up doing was adding an alpha band to the original VRT, by keeping a white PNG around and using it to augment the existing RGB bands from the JPEG source files. This worked well, the results are as I'd expect, with the side effect that I'm now building up the VRT files from strings instead of shelling out to gdal_translate as I was doing before. I'm satisfied with the outcome, and after figuring out the difference between RGBA and ARGB I'm getting correct-looking images with aliased alpha channel output.

Here's a sample VRT:
	http://dpaste.com/hold/512864/


> Another approach might be for the me to modify GDALReprojectImage to
> utilize an alpha band properly if it exists on the destination image.
> I can do this in trunk if that would be helpful to you.


A more featureful ReprojectImage would definitely be helpful in the future, though for now I think I've gotten to an outcome that works well enough.

Thanks for an excellent library!

-mike.

----------------------------------------------------------------
michal migurski- mike at stamen.com
                 415.558.1610





More information about the gdal-dev mailing list