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

Frank Warmerdam warmerdam at pobox.com
Wed Mar 16 12:58:55 EDT 2011


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?

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.

So one approach is for you to precreate the output image, initializing
with some particular nodata pixel value and then using that in post
processing.

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.

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    | Geospatial Programmer for Rent



More information about the gdal-dev mailing list