I have a downloaded raster file, singlebandgray, and want to redefine it's maximum value to a higher integer value. Via the QGIS desktop it works smoothly by hand via the Properties. Via Python I was not able to figure it out. I tried something like this (some snippets that I found somewhere)

fcn = QgsColorRampShader()
shader = QgsRasterShader()shader.setRasterShaderFunction(fcn)
renderer = QgsSingleBandGrayRenderer(rasterlayer.dataProvider(), 1, shader)

The raster file is saved as "rasterlayer". Its minimum value is 0. The maximum value should be 66 instead of a lower integer. Everything else should stay equal.

I should add that I am not looking for a solution which loads the style with an external .qml file. I tried that and it works fine. But it is not my desired solution.

1 Answer 1


Try using contrast enhancement to stretch the gray scale across a specified interval.

# create a single band gray scale renderer with gray scale stretched
# across the interval [0..66]
renderer = QgsSingleBandGrayRenderer(rasterlayer.dataProvider(), 1)
ce = QgsContrastEnhancement(rasterlayer.dataProvider().dataType(0))

  • I'm still struggling with all this different methods. Feels like a jungle for me. Do you have a tip on how to build intuition to come up with a solution like yours? I don't find all this PyQGIS books very helpful since they usually just give recipies to specific examples without giving you a big picture. Jan 25, 2016 at 7:16
  • 1
    @crunchbangax not really. I am currently working on a Qgis development project, being often faced with this problem. Somewhere between understanding the C++ oriented documentation, python related questions, structuring the scientific problem - lays a possible solution. I am maintaining my own repo of small working scripts, saving smart solutions from others, published somewhere. What would be nice to have are cheats, overview posters of classes and methods in context of real world tasks. Maybe it's a good idea to get active and start
    – Detlev
    Jan 25, 2016 at 8:29
  • I highly agree! :) Would something like a history/log file for QGIS Desktop help to build an intuition for "broader masses"? (see gis.stackexchange.com/questions/177978/…) Jan 25, 2016 at 8:38
  • 1
    Maybe, but do not expect to much from such log files. Since Qgis core is written in C++ it is of little help to know, what library functions are called. It is important to know python API. When plugins or modules from processing environment are being called, it would be more helpful, but if you want to script your own tools, you must have a look behind the curtain. For example I study the source of plugins to learn how they work, and search for text strings in py files to find what is actually being called when I can not get this info otherwise
    – Detlev
    Jan 25, 2016 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.