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.
2 Answers 2
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()
-
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?willsbit– willsbit2020年08月17日 02:22:41 +00:00Commented Aug 17, 2020 at 2:22 -
Try now, I've changed the code!Bera– Bera2020年08月17日 06:19:01 +00:00Commented 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 nothingwillsbit– willsbit2020年08月17日 13:18:32 +00:00Commented Aug 17, 2020 at 13:18
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.