Image entropy from PIL histogram


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.