3

I have many raster layers in my QGIS project and they're all currently rendered as: Multiband color Red band: Band 1 Green band: Band 2 Blue band: Band 3

I'd like to know if there's any way I can change the rendering of all raster layers to: Red band: Band 3 Green band: Band 2 Blue band: Band 1 without having to do it by hand, layer by layer.

Bera
81.7k14 gold badges85 silver badges199 bronze badges
asked Aug 16, 2020 at 18:03

2 Answers 2

3

You can use pyqgis. This will change symbology of all raster layers in layer tree to red band=band 3, green=band 2, blue=band 1:

for lyr in QgsProject.instance().mapLayers().values():
 if isinstance(lyr, qgis.core.QgsRasterLayer): #Check to make sure it is a raster layer
 r = lyr.renderer().clone()
 r.setRedBand(3)
 r.setGreenBand(2)
 r.setBlueBand(1)
 lyr.setRenderer(r)
 lyr.triggerRepaint()

enter image description here

answered Aug 16, 2020 at 19:04
3
  • I'm sorry to bother you, but I've ran into some trouble. The bands change accordingly, but the changes don't apply unless I go into each layer's symbology and hit the "Apply" button. I tried using triggerRepaint() and other similar methods like qgis.utils.iface.mapCanvas().refresh() but they don't seem to work. Any help? Commented Aug 17, 2020 at 2:22
  • Try now, I've changed the code! Commented Aug 17, 2020 at 6:19
  • The same thing still happens. I think it has something to do with applying the "stretch to min-max" contrast settings. I tried working around it with something like this: layer = iface.activeLayer() contrastFilter = QgsBrightnessContrastFilter() contrastFilter.setContrast(50) layer.pipe().set(contrastFilter) layer.triggerRepaint() but it basically does nothing Commented Aug 17, 2020 at 13:18
3

Set it up for one layer.

Right click that layer, Styles -> Copy Style.

Select all the other layers you want to apply the same style to, right-click, Styles -> Paste Style.

answered Aug 17, 2020 at 18:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.