pythonnumpyimagemagickpython-imaging-libraryosgeo

Adding alpha to .dds file


how can I add to a .dds file (without alpha), an alpha channel? The source for the alpha data, is a 8bit gray tiff. I tried gdal_merge but it seems that adds transparency to the rgb and not a new alpha channel. Thank you!


Solution

  • You can put a greyscale image into the alpha channel of an image using ImageMagick, which is installed on most Linux distros and is available for macOS and Windows, like this:

    magick input.dds greyscale.tif -compose copyopacity -composite result.dds
    

    The input images could equally be TIF, GIF, or JPEG and the output image could be PNG or anything that supports transparency.


    Here's an example. Create a magenta input image:

    magick -size 600x400 xc:magenta input.dds
    

    enter image description here

    Make a black-white gradient greyscale TIF:

    magick -size 600x400 gradient: alpha.tif
    

    enter image description here

    Use greyscale gradient as alpha layer in magenta original:

    magick input.dds alpha.tif -compose copyalpha -composite result.dds
    

    enter image description here


    If you are using v6, or older, of ImageMagick, replace all occurrences of magick in my command lines above with convert.