I'd like to re-visit this answer given by Aaron that uses
numpy to calculate the mean value of each band.
Issue 1: With large TIFFs ~ 5-15GB, I am running into a memory error. I am wondering if gdal is uncompressing the TIFF and making it explode?
Issue 2: I switched to a lower resolution and it does run, but the results are different comparing to what QGIS gives in the layer properties. I tired calculating with and without the zero's, but still no match. With zero's is closer, but is still off by about 3 units.
I understand that larger TIFFs will take a bit longer to processes, but it seems faster to plop them into QGIS and get the values from there. Trying to find a "just as fast" automated solution.
I am open to using
Tiffile, or other raster libraries.
def rgb(ortho): raster = gdal.Open(ortho) bands = raster.RasterCount for band in range(1, bands + 1): data = raster.GetRasterBand(band).ReadAsArray().astype('int') mean = np.mean(data[data != 0]) # calculate mean without value 0 print("Band %s: Mean = %s" % (band, round(mean, 2)))
numpy.core._exceptions.MemoryError: Unable to allocate 42.1 GiB for an array with shape (90880, 62208) and data type float64
QGIS Mean Properties:
Python Output with zero's:
Python Output without zero's