I have few hundred raster files and I want to count the number of pixels having values within a specific range. Following code works well when using ==
operator e.g., raster_file == 25
, but gives totally wrong results when I use a range e.g., (raster_file >= 0) & (raster_file <= 25)
import os, sys
import numpy as np
import gdalnumeric
path = ('dir')
os.chdir(path)
for rasters in os.listdir(path):
raster_file = gdalnumeric.LoadFile(rasters)
pixel_count = ((raster_file =>0) & (raster_file <= 25)).sum()
print(pixel_count)