def shannon_entropy(img): # calculate the shannon entropy for an image histogram = img.histogram() histogram_length = sum(histogram) samples_probability = [float(h) / histogram_length for h in histogram] return -sum([p * math.log(p, 2) for p in samples_probability if p != 0])