I need to know the value of the sum of all pixels in a raster. However, in the Statistics section of the Metadata tab in Properties, under Sum of All Cells it always indicates 0.00. I have tried with different raster formats to no avail. What am I doing wrong? How can I get this value?

  • I hope I got you right; I tried to reconstruct your problem with an Raster of mine. The sum of all pixels in a raster is the x-pixels*y-pixels, right? This value you can get in 2 ways: 1)Right click on the actual File(a tiff in my case), the detail window should provide this information 2)Inside the Gis click Raster->Miscellaneous->Information. Select your file->edit->ok. Then you should the the two values, that have to be multiplied. The result is the sum of all pixels in a raster.
    – Shepherdjo
    Commented Jun 20, 2012 at 6:28
  • Wrong. That would give you the number of cells. Not the sum of cell values Commented Jan 26, 2015 at 0:13
  • Let me make an assumption of why you might need this... You are trying to calculate the volume of a DEM? In which case, you can multiply the pixel_size^2 (pixel area) by the pixel height (or depth), for each pixel, and sum the results. But Sum[pix_area * pix_height] = pix_area * Sum[pix_height], if the pixel area is constant, correct? Therefore, you can get the volume by multiplying the sum of the pixel values (assuming the pixel value is the elevation) with the pixel area. If that's what you were trying to do, you could edit it into the question; that might make it even more useful to readers Commented Aug 20, 2023 at 13:11

5 Answers 5

  1. Create a polygon using Vector > Research Tools >Polygon from layer extent. here layer is the raster in question.
  2. Activate/ Install Zonal statistics Plugin.
  3. Use Zonal statistics plugin where the raster is the one whose sum is required and the polygon is the one you created from layer extent. Enter output column prefix (say stats_). Run the plugin.
  4. Open the attribute table of the polygon. Now you have stats_sum column along with other columns with stats_ prefix.
  5. I checked the values in GRASS GIS and the match is perfect.

In QGIS 2.x, you can get raster statistics for the entire layer (including the sum of all pixel values in the band) by using the Raster layer statistics algorithm in the Processing toolbox.

Processing | Toolbox (check-box)

then go to the Processing toolbox:

QGIS geoalgorithms | Raster tools | Raster layer statistics

Generate the statistics for the layer you want to inspect. The results are then stored in the layer metadata, which is accessible by right-clicking on the layer and choosing Properties.


You can use pyqgis and create a numpy array and sum:

import gdal
import numpy as np
ds = gdal.Open(r'C:\somefolder\someraster.tif')
band1 = ds.GetRasterBand(1).ReadAsArray()
s = np.sum(band1)

I understood the question as, How do I get sum of all pixel values of a raster? Something like running Statistics on the attributes of a vector. One can do this easily via programming. I can think of another way to do this via QGIS. It would be inefficient and slightly inaccurate, but it will work.

  • Create a point layer representing each pixel of the raster. You can do this via Vector -> Research Tools -> Regular Points. Use number of points as x_size * y_size. So for a raster of 100 px width x 100px height, use 10000. Specify an offset of (pixel resolution)/2 , so you are sampling a the center of the pixel.
  • Next, install the Point Sampling plugin. Using this plugin, you can sample the raster at all of the 10000 points and get an attribute value which is the pixel value.
  • Run Vector -> Analysis -> Basic Statistics on the resulting vector to get the sum.

You could use gdalinfo to get the raster statistics, for example:

gdalinfo -stats filename.tif

This will give you the mean pixel value and the raster size in pixels. You could also get this data from the Properties -> Metadata tab.

Then it's a question of arithmetic: mean value x (height in pixels x width in pixels).


  • 5
    That is wrong. First, these stats are not always present (depends on the application that wrote the file) and if they are present, you cannot be sure that they are actually correct. Second, the multiplication ignores the possibility that there are No Data values (in which case this computation would be wrong). Commented Jan 26, 2015 at 0:12

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.