简体   繁体   中英

How to use GetPixel() function in GDAL Python for GeoTiff Images?

We need to get the Pixel value for the GeoTIFF images to calculate the mean.

In Python we use GetPixel() to know the each pixel values but which function should we use in GDAL python.

Help me Out!

In the simplest case you can simply use ReadAsArray() and numpy's average function:

import numpy
from osgeo import gdal

ds = gdal.Open(tiffilepath)
data = ds.ReadAsArray().astype(numpy.float32) # Type depends on the type you want for the average.
avg = numpy.average(data)
print(avg)

This statistic also likely exists in the metadata, but may not be accurate, and you may wish to calculate it differently (including/excluding no data values, background values, etc.).

If you want to get a limited number of pixels use gdallocationinfo

gdallocationinfo input.tif 250 150

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM