I have almost 100 of single band grayscale rasters. Importing all of them one by one to QGIS and changing the color ramp seems laborious. Is there any PyQgis codes/function that can apply predefined color ramp to a raster? I found multiple solutions where python defines the color ramp first then apply it to the raster. Here is a great example of creating a color ramp and assigning it to raster. The CookBook has a similar example, which is -
>>> fcn = QgsColorRampShader() >>> fcn.setColorRampType(QgsColorRampShader.INTERPOLATED) >>> lst = [ QgsColorRampShader.ColorRampItem(0, QColor(0,255,0)), \ QgsColorRampShader.ColorRampItem(255, QColor(255,255,0)) ] >>> fcn.setColorRampItemList(lst) >>> shader = QgsRasterShader() >>> shader.setRasterShaderFunction(fcn)
However, I don't want to define the color ramp. My choice of color ramp is in predefined, which is "RdBu" (Red to Blue) on a raster layer. Is there any way to do it?