I am trying to replicate using Python, what QGIS does to visualize the rasters in the map.
QGIS automatically chooses as contrast enhancement the method Stretch to MinMax. Looking at the Min/Max Settings, the Cumulative count cut is selected with the range being between 2% - 98%.
I saw in this answer that the cumulative count cut method is basically the same than numpy.nanpercentile. So I implemented this function in python:
def cumulative_count_cut(band, min_percentile=2, max_percentile=98):
min_val = np.nanpercentile(band, min_percentile)
max_val = np.nanpercentile(band, max_percentile)
print(min_val,max_val)
I was expecting to the Min and Max values equal or even near the ones in QGIS but this is what I got instead:
- Band 04: Min: 0.0 | Max: 1221.0
- Band 03: Min: 0.0 | Max: 1430.0
- Band 02: Min: 0.0 | Max: 1663.0
Am I missing something? Shouldn't the values be the same?

