2

I'm working with a RGB raster on QGIS 2.6.1, on a Windows 7 machine.

I need to set the Contrast Enhancement option to No Enhancement with the Python Console (not through the Layer Properties window). I have developed a simple script, inspired by this discussion: QGIS Scripting problem updating contrast enhancements for raster layers using bandStatistics

layer = iface.activeLayer()
renderer = layer.renderer()
ContrastEnhancement = QgsContrastEnhancement.NoEnhancement

for Band in range(3):
    if Band == 0:
        myBand = layer.renderer().redBand()
    elif Band == 1:
        myBand = layer.renderer().greenBand()
    elif Band == 2:
        myBand = layer.renderer().blueBand()
    myType = layer.renderer().dataType(myBand)
    myEnhancement = QgsContrastEnhancement(myType)
    myEnhancement.setContrastEnhancementAlgorithm(ContrastEnhancement,True)
    if Band == 0:
        layer.renderer().setRedContrastEnhancement(myEnhancement)
    elif Band == 1:
        layer.renderer().setGreenContrastEnhancement(myEnhancement)
    elif Band == 2:
        layer.renderer().setBlueContrastEnhancement(myEnhancement)

layer.triggerRepaint()

Unfortunately, it does not work and the layer becomes not visible. It seems that it is not handling correctly the min/max values of the bands. Any suggestion?

  • You need a 'ContrastEnhancement' for each band to 'StretchToMinimumMaximum' instead of 'NoEnhancement'. – xunilk Dec 28 '15 at 10:35
2

Your script works as it is expected because you need a 'ContrastEnhancement' for each band to 'StretchToMinimumMaximum' instead of 'NoEnhancement'. I modified your script to calculate minimum and maximun values for each band by using 'bandStatistics' method from QgsDataProvider class. These values were set manually at the code and the rgb image 'natural_earth' was used to test it.

layer = iface.activeLayer()
renderer = layer.renderer()
provider = layer.dataProvider()

bands = renderer.usesBands()

min = [provider.bandStatistics(band, QgsRasterBandStats.All).minimumValue 
            for band in bands]

max = [provider.bandStatistics(band, QgsRasterBandStats.All).maximumValue 
            for band in bands]

print min
print max

ContrastEnhancement = QgsContrastEnhancement.StretchToMinimumMaximum

myRedBand = layer.renderer().redBand()
myRedType = layer.renderer().dataType(myRedBand)
myRedEnhancement = QgsContrastEnhancement(myRedType)
myRedEnhancement.setContrastEnhancementAlgorithm(ContrastEnhancement,True)
myRedEnhancement.setMinimumValue(62)
myRedEnhancement.setMaximumValue(255)
layer.renderer().setRedContrastEnhancement(myRedEnhancement)

myGreenBand = layer.renderer().greenBand()
myGreenType = layer.renderer().dataType(myGreenBand)
myGreenEnhancement = QgsContrastEnhancement(myGreenType)
myGreenEnhancement.setContrastEnhancementAlgorithm(ContrastEnhancement,True)
myGreenEnhancement.setMinimumValue(89)
myGreenEnhancement.setMaximumValue(255)
layer.renderer().setGreenContrastEnhancement(myGreenEnhancement)

myBlueBand = layer.renderer().blueBand()
myBlueType = layer.renderer().dataType(myBlueBand)
myBlueEnhancement = QgsContrastEnhancement(myBlueType)
myBlueEnhancement.setContrastEnhancementAlgorithm(ContrastEnhancement,True)
myBlueEnhancement.setMinimumValue(90)
myBlueEnhancement.setMaximumValue(255)
layer.renderer().setBlueContrastEnhancement(myBlueEnhancement)

layer.triggerRepaint()

After running the code at the Python Console of QGIS not changes were observed at the raster (see next image); as it expected.

enter image description here

However, if it is modified, for example, the maximum value for the red enhancement to 100:

myRedEnhancement.setMaximumValue(100)

the result changes drastically; as it can be observed at the next image:

enter image description here

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.