I'm exploring QGIS python functions to set all raster layers with a custom transparency, i.e 0,0,0,100 .

Looking at some previous questions. There is a way to loop all the layers, but the aspect to set transparency value on RGB values, I've been having difficulties understanding the documentation.

How to set the transparency for multiple layers or add a global transparancy preference?

for layer in iface.legendInterface().layers():

How do I set layer transparency in QGIS 2.0 with Python? The example here uses TransparentSingleValuePixel but i notice there is a TransparentThreevaluePixel

x = QgsRasterTransparency.TransparentSingleValuePixel()
x.pixelValue = 123
x.transparencyPercent = 50
rt.setTransparentSingleValuePixelList( [ x ] )
  • I don't think I completely understand what you want to do. Do you want to change the raster (source) files or alter the properties of how the layers are displayed? For the latter: why would you want to specify also R, G and B values? – christoph Jul 14 '15 at 19:20
  • I think its the way the layers are rendered. Not altering the image file itself. I have a border of nodata around the image with 0,0,0. It's overlapping on other images. The aim simply to choose the 0,0,0 and set to 100% opacity. – Pat Jul 15 '15 at 0:27

You want to use a QgsRasterTransparency object then: http://qgis.org/api/2.8/classQgsRasterTransparency.html (add it to the renderer() of the raster layer using setRasterTransparency())


# -> [<qgis._core.TransparentThreeValuePixel object at 0x117dab5a8>]


voilà: same result as adding them by hand in the layer properties:

voilà: same result as adding them by hand in the layer properties:

  • to extend on this code here, I apply to this to all layers with the following code for r in iface.mapCanvas().layers(): rt.QgsRasterTransparency(); rt.InitializeTransparentPixelList(0,0,0); r.renderer().setRasterTransparency(rt); r.triggerRepaint(); – Pat Jul 16 '15 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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