Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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