[Qgis-developer] Contrast Enhancement

alexia.mondot at free.fr alexia.mondot at free.fr
Wed Nov 26 04:30:35 PST 2014


Hi, 

Thanks for your reply.

I've contributed to this plugin :P
The line you pointed to me is the line setting the renderer.
It is used to set the X contrast enhancement.
In my actual code, 
i had 
renderer = theRasterLayer.renderer()
renderer.setRedContrastEnhancement( redEnhancement) 
renderer.setGreenContrastEnhancement( greenEnhancement ) 
renderer.setBlueContrastEnhancement( blueEnhancement )

I changed it to :
theRasterLayer.renderer().setRedContrastEnhancement( redEnhancement) 
theRasterLayer.renderer().setGreenContrastEnhancement( greenEnhancement ) 
theRasterLayer.renderer().setBlueContrastEnhancement( blueEnhancement)
to test if maybe it was a problem with my object renderer. I didn't changed anything, but I've let this writing.

Regards, 
Alexia


----- Mail original -----

Nejia <nejia at spaceyes.fr> a écrit :

> Hi,
>
>
>
> You can see ?Contrast Homogenizer? plugin that you can download it 
> from Qgis Plugins - Official Repository 
> <http://plugins.qgis.org/plugins/plugins.xml?qgis=2.6>
>
> File contrastHomogenizer.py line 249
>
>
>
> Regards,
>
> Nejia
>
>
>


De: "Alexia Mondot" <alexia.mondot at free.fr>
À: qgis-developer at lists.osgeo.org
Envoyé: Mercredi 26 Novembre 2014 01:07:44
Objet: [Qgis-developer] Contrast Enhancement


Hi all, 

I have some problems to apply new stretching values to my layer 

I have a function which allows to adapt the stretching of my image without the raster layer properties dialog. 

When I open an image with my plugin, I display it on the canvas, I open my own histograms, and I can move the min max stretch of the 3 displayed bands. 

It works well. But, when i save the project, i reopen it, I want to change the image stretching, it does nothing. The values are set, but it seems that the layer renderer is not updated. 

Here is my stretching code : 



print canvas 
print "layer :", theRasterLayer 

# type of layer : raster, vector, other 
typeOfLayer = theRasterLayer.type() 

dataProvider = theRasterLayer.dataProvider() 

# the layer has to be a raster layer 
if typeOfLayer == 1 : 
if (theRasterLayer.rasterType() == 0 ) and theRasterLayer.renderer(): 
[...] 

elif theRasterLayer.rasterType() == 2 and theRasterLayer.renderer(): 
min_red, max_red = values[0] 
min_green, max_green = values[1] 
min_blue, max_blue = values[2] 

redEnhancement = QgsContrastEnhancement( dataProvider.dataType( 0 ) ) 
greenEnhancement = QgsContrastEnhancement( dataProvider.dataType( 1 ) ) 
blueEnhancement = QgsContrastEnhancement( dataProvider.dataType( 2 ) ) 
#set stretch to min max 
redEnhancement.setMinimumValue( min_red ) 
redEnhancement.setMaximumValue( max_red ) 
greenEnhancement.setMinimumValue( min_green ) 
greenEnhancement.setMaximumValue( max_green ) 
blueEnhancement.setMinimumValue( min_blue ) 
blueEnhancement.setMaximumValue( max_blue ) 
redEnhancement.setContrastEnhancementAlgorithm(1) 
greenEnhancement.setContrastEnhancementAlgorithm(1) 
blueEnhancement.setContrastEnhancementAlgorithm(1) 

print "blue enhancement", blueEnhancement 
print "blue max", blueEnhancement.maximumValue() 
print "blue min", blueEnhancement.minimumValue() 

theRasterLayer.renderer().setRedContrastEnhancement( redEnhancement) 
theRasterLayer.renderer().setGreenContrastEnhancement( greenEnhancement ) 
theRasterLayer.renderer().setBlueContrastEnhancement( blueEnhancement) 

theRasterLayer.triggerRepaint() 

print theRasterLayer.renderer().blueContrastEnhancement() 
print theRasterLayer.renderer().blueContrastEnhancement().minimumValue() 
print theRasterLayer.renderer().blueContrastEnhancement().maximumValue() 
canvas.refresh() 
canvas.repaint() 
In my case, the canvas, is the main canvas, I have checked I work on the good one comparing the print canvas and the python console with iface.mapCanvas(). 

I know : 
* both cases run this function. 
* the minimum and maximum values of contrast enhancement are the same in my two prints (before and after the setXContrastEnhancement) (so the theRasterLayer.renderer().blueContrastEnhancement() seems to be well set) 
* these values are the good ones 
* they aren't displayed in the raster layer properties dialog 
* my raster layer source is ok 
* when i do these steps on the QGIS python console with the layer iface.mapCanvas().currentLayer() which points to the same file, it works 



layer = iface.mapCanvas().currentLayer() 
blue_en = QgsContrastEnhancement( layer.dataProvider().dataType(2)) 
blue_en.setMinimumValue(150) 
blue_en.setMaximumValue(350) 
blue_en.setContrastEnhancementAlgorithm(1) 
layer.renderer().setBlueContrastEnhancement(blue_en) 
layer.triggerRepaint() 
What have I missed ? 

Thanks by advance, 
Alexia 

_______________________________________________
Qgis-developer mailing list
Qgis-developer at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the Qgis-developer mailing list