2

I'm trying to find a way to change these properties (especially contrast and brightnes) and I've found a QgsBrightnessContrastFilter class, but I can't fugure out how to apply it to my raster layer.

I think that can be done with:

contrastFilter = QgsBrightnessContrastFilter()
contrastFilter.setContrast(100)

But what now? How to set this filter to my layer?

  • If I need to assign to multiple layer, how can avoid the qgis crash? – marcus Jun 12 '18 at 8:34
2

After crawling through the source code I got this:

# get the raster layer
layer = iface.activeLayer()

# define the filter
contrastFilter = QgsBrightnessContrastFilter()
contrastFilter.setContrast(50)

# assign filter to raster pipe
layer.pipe().set(contrastFilter)

# apply changes
layer.triggerRepaint()

# to set contrast back, change properties of assigned filter.
# DO NOT ASSIGN THIS OR ANOTHER QgsBrightnessContrastFilter AGAIN, QGIS WILL CRASH
contrastFilter.setContrast(0)
layer.triggerRepaint()

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.