Posted By

vasilije on 09/28/09


Tagged

python PIL


Versions (?)

Image entropy from PIL histogram


 / Published in: Python
 

  1. from math import log
  2.  
  3. def get_histogram_dispersion(histogram):
  4. log2 = lambda x:log(x)/log(2)
  5.  
  6. total = len(histogram)
  7. counts = {}
  8. for item in histogram:
  9. counts.setdefault(item,0)
  10. counts[item]+=1
  11.  
  12. ent = 0
  13. for i in counts:
  14. p = float(counts[i])/total
  15. ent-=p*log2(p)
  16. return -ent*log2(1/ent)

Report this snippet  

You need to login to post a comment.