Skip to content

Instantly share code, notes, and snippets.

@mtrimpe
Created October 19, 2012 16:19
Show Gist options
  • Save mtrimpe/3919144 to your computer and use it in GitHub Desktop.
Save mtrimpe/3919144 to your computer and use it in GitHub Desktop.
Rename images to sort by entropy (PIL needed)
import Image
import os
from math import log
def entropy(img):
histogram = img.histogram()
log2 = lambda x:log(x)/log(2)
total = len(histogram)
counts = {}
for item in histogram:
counts.setdefault(item,0)
counts[item]+=1
ent = 0
for i in counts:
p = float(counts[i])/total
ent-=p*log2(p)
return -ent*log2(1/ent)
for file in os.listdir('.'):
os.rename(file, str(entropy(Image.open(file))) + '-' + file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment