Created
October 19, 2012 16:19
-
-
Save mtrimpe/3919144 to your computer and use it in GitHub Desktop.
Rename images to sort by entropy (PIL needed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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