I would like to be able to extract the same values that QGIS obtains automatically when calculating the 2% and 98% values for a raster in GDAL. But GDAL only yields the result for the minimal and maximum value of the pixel as -1 and 1.

Someone has any idea of what's happening here?


QGIS VALUES Min: -0.052 | Max: 0.254


enter image description here

For obtain the gdal information i used :

gdalinfo -mm RASTER 

But I am not looking for the Min and Max values for the raster, but for the Cumulative count cut. My file is a GeoTiff

The GeoTiff is a Raster of NDVI values.

  • 1
    Please update your question with 1) the data type you are trying to read (is it a geotiff?) , 2) the gdal code , 3) the python code. Regarding the python: you get the same values as in gdal or in qgis?
    – ylka
    Commented Nov 24, 2016 at 13:31
  • For obtain the gdal information i used : gdalinfo -mm RASTER But i don't looking for the min an max values for all raster i need the min and max data of the all raster. The Cumulative count cut. Yes is a GEOTif Thx Commented Nov 24, 2016 at 14:52

2 Answers 2


You can achieve this in python with numpy.percentile:

import numpy as np
from osgeo import gdal

ds = gdal.Open(<<raster_filename>>)
arr = gdal.ReadAsArray()

percentile2 = np.percentile(arr, 0.02)
percentile98 = np.percentile(arr, 0.98)

ds = arr = None

gdalinfo can not actually provide you the cumulative count, but you can do this with gdal_contrast_stretch, such as:

gdal_contrast_stretch -percentile-range 0.02 0.98 image_input.tif image_out.tif

This will stretch your image but will also output the values you are looking for on the console. You can then delete the output file as needed.

Hope this helps!

Installation guide: https://github.com/gina-alaska/dans-gdal-scripts

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.