In Photoshop you can use a mask layer to filter an image using another image. A mask layer in Photoshop is a greyscale image attached to a layer that determines that layers transparency per pixel. White is fully opaque, black is fully transparent. This method can be used for example to filter a curvature layer by a slope layer.
Would it be possible to have the same result in QGIS using the raster calculator or other raster processing tools?

Applying the following GDAL commands:

gdal_translate -of VRT -b 1 input.tif band1.vrt  
gdalbuildvrt -separate out_with_mask.vrt band1.vrt your_mask.tif  
gdal_translate -of GTiff -co alpha=YES out_with_alpha.vrt out_with_alpha.tif  

and testing the obtained file I have:

Band 1 Block=1808x1 Type=Float32, ColorInterp=Gray  


Band 2 Block=1808x1 Type=Float32, ColorInterp=Alpha

but I don't get the target file masking using transparency values based on the mask file, I just get a full transparency effect on the mask file:
enter image description here


1 Answer 1


I do not know any GDAL or QGIS tool that could add alpha channel directly but it is possible to do by performing some tricks with GDAL virtual raster format (VRT).

  1. Create a separate virtual raster from each band of the source image.
gdal_translate -of VRT -b 1 input.tif band1.vrt
gdal_translate -of VRT -b 2 input.tif band2.vrt 
gdal_translate -of VRT -b 3 input.tif band3.vrt
  1. Combine these three bands and your mask into a 4-band VRT

gdalbuildvrt -separate out_with_mask.vrt band1.vrt band2.vrt band3.vrt your_mask.tif

  1. Materialize the 4 band virtual raster into GeoTIFF

gdal_translate -of GTiff -co photometric=RGB -co alpha=YES out_with_alpha.vrt out_with_alpha.tif

  1. Test the result with gdalinfo
gdalinfo out_with_alpha.tif
Driver: GTiff/GeoTIFF
Files: out_with_alpha.tif
Size is 4692, 4151
Image Structure Metadata:

Band 1 Block=4692x1 Type=Byte, ColorInterp=Red
Band 2 Block=4692x1 Type=Byte, ColorInterp=Green
Band 3 Block=4692x1 Type=Byte, ColorInterp=Blue
Band 4 Block=4692x1 Type=Byte, ColorInterp=Alpha

Answer is based on this mail in the gdal-dev list https://www.mail-archive.com/[email protected]/msg26992.html.

  • Thanks, if I understand correctly it is about adding the masking raster file as an alpha channel to the original file, and I have some questions about it: is it always mandatory to transform the source file into a tif rgb file even when it is a grayscale file? Using a grayscale mask file will the transparency in the alpha channel be proportional to the point-by-point values (not only fully opaque or fully transparent)? Should I run the commands you suggest from within QGIS (how)?
    – Andrew-63
    Mar 10, 2021 at 9:55
  • You can add alpha to single band image as a second band. How alpha is used depends on the viewer. Some geospatial viewers consider that alpha is binary, zero vs. anything greater than zero. Some others may blend like Photoshop does. I do not know how QGIS behaves, in GIS images the alpha values are usually either 0 or 255. You can make a test TIFF with your Photoshop and try. Commands are for command line, documentation is at gdal.org.
    – user30184
    Mar 10, 2021 at 11:32
  • I have tried to use your suggestion but unfortunately the result is not what I expected. I updated the question, any idea about it?
    – Andrew-63
    Mar 22, 2021 at 10:52
  • As I said how it looks like depends on the viewer. QGIS is now probably using alpha as mask with two values: zero or anything above zero. However, you want to do alpha blending. I can see that there are different blending modes in QGIS but I do not really know how to use them.
    – user30184
    Mar 22, 2021 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.