4

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():
   layer.renderer().setOpacity(0.2)

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 ] )
2
  • 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? Commented Jul 14, 2015 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. Commented Jul 15, 2015 at 0:27

1 Answer 1

4

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())

r=iface.mapCanvas().layers()[0]
rt=QgsRasterTransparency()
rt.initializeTransparentPixelList(0,0,0)

QgsMessageLog.logMessage(str(rt.transparentThreeValuePixelList()))
# -> [<qgis._core.TransparentThreeValuePixel object at 0x117dab5a8>]

r.renderer().setRasterTransparency(rt)

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

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

1
  • 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(); Commented Jul 16, 2015 at 2:11

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.