[Qgis-user] Qgis : Calculat the median of multiple bands of raster (90 bands)

chris hermansen clhermansen at gmail.com
Tue Dec 23 15:35:58 PST 2025


Laurent and list,

On Tue, Dec 23, 2025, 15:23 celati Laurent via QGIS-User <
qgis-user at lists.osgeo.org> wrote:

> Good afternoon,
>
> I work with Qgis 3.40.
>
> I have a question almost similar to this post
> <https://gis.stackexchange.com/questions/338888/calculating-the-average-of-multiple-bands-of-raster-with-high-number-of-bands-in>.
> But i would like to caculate the median instead of the average/median. From
> one single raster with 90 bands, i would like generate a new raster file
> with the median value for each pixel. The median has to be calculated from
> all the 90 bands for each pixel.
>
> Following still this post
> <https://gis.stackexchange.com/questions/338888/calculating-the-average-of-multiple-bands-of-raster-with-high-number-of-bands-in>succeed
> in calculating the average from the 90 bands thanks to this following
> python qgis script :
>
>     rasterlayer = QgsProject.instance().mapLayersByName('infra-red')[0]
>> #Change infra-red to the name of your layer
>>     bandcount = rasterlayer.bandCount() #Count the bands
>>
>>     tempexp = ['"{0}@{1}"'.format(rasterlayer.name(), bandnum) for
>> bandnum in range(1,bandcount+1)] #['"infra-red at 1"', '"infra-red at 2"',
>> '"infra-red at 3"', '"infra-red at 4"']
>>     tempexp = '+'.join(tempexp) #'"infra-red at 1"+"infra-red at 2
>> "+"infra-red at 3"+"infra-red at 4"'
>>     exp = '('+tempexp+')/{0}'.format(bandcount) #'("infra-red at 1
>> "+"infra-red at 2"+"infra-red at 3"+"infra-red at 4")/4'
>>
>>     processing.runAndLoadResults("qgis:rastercalculator",
>>
>> {'EXPRESSION':exp,'LAYERS':[rasterlayer.source()],'CELLSIZE':None,'EXTENT':None,'CRS':None,'OUTPUT':'TEMPORARY_OUTPUT'})
>
>
> Is there a way to do the same thing but this time to calculate the median
> value ?
>

Not sure of the actual expression but if you can sort the list ¿? created
in the join step then you must extract elements 45 and 46 and take their
average to get the median of a list with 90 elements.

> <https://lists.osgeo.org/mailman/listinfo/qgis-user>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20251223/36a912b6/attachment.htm>


More information about the QGIS-User mailing list