After doing calculations on a raster and saving it, the image statistics are wrong and need updating.

I find examples saying that this should do the job, like in this example:


However this does not seem to update my band statistics correctly, they seem ok in Python with data.max() and data.min(), but opening the file in QuantumGis (image properties>style>load min/max) shows the wrong stats. I only get new image statistics correctly by separately opening and setting statistics like this:

driver = gdal.GetDriverByName('GTiff')
ds = gdal.Open(infile, gdal.GA_Update)
dsband = ds.GetRasterBand(1)
(newmin, newmax)= dsband.ComputeRasterMinMax(0)
(newmean, newstdv) = dsband.ComputeBandStats(1)
dsband.SetStatistics(newmin, newmax,newmean, newstdv)
dsband = None  
ds = None

So what's the difference between both and why does QGis only show the correct stats with the second code?

thanks for any advice!

2 Answers 2


The following code works for me (in that QGis reads the statistics correctly). Note use of ComputeStatistics instead of GetStatistics as that will force gdal to recalculate band statistics.

driver = gdal.GetDriverByName( 'GTiff' )
dst_ds = driver.Create( dst_filename, 512,512, 1, gdal.GDT_UInt16 )
raster = somenumpy.calculation(etc...)
rb.WriteArray( raster )
dst_ds = None

I had a similar issue where there were differences between statistics in Python and QGIS for the same tile/data. I'm rather new to this so I can't offer an explanation as to why but I managed to get the correct values displayed in QGIS by going to properties > symbology > Min/Max value settings and then setting 'Accuracy' to 'Actual (slower)'.

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.