Get the quantile of raster image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# -*- coding: utf-8 -*-
import tifffile as tiff
import math
import numpy
from itertools import chain
img = tiff.imread('tiffilePath')
listImg = list(chain.from_iterable(img))
#Filter out negative numbers (optional)
x = numpy.array([num for num in listImg if num >= 0])
def percentile(data, perc: int):
size = len(data)
return sorted(data)[int(math.ceil((size * perc) / 100)) - 1]

#5th percentile
print("5th percentile:"+str(percentile(x, 5)))
#95th percentile
print("95th percentile:"+str(percentile(x, 95)))