如何用Python Imaging Library(PIL)识别非照片或‘无趣’图像
我有成千上万张图片,我需要把那些不是照片或者不太“有趣”的图片挑出来。
比如说,一张“无趣”的图片可能就是全是一个颜色,或者大部分是一个颜色,或者只是一个简单的图标/标志。
这个解决方案不需要完美,只要能把那些最无趣的图片去掉就行。
到目前为止,我最好的主意是随机抽取一些像素,然后……对它们做点什么。
1 个回答
2
Danphe比我先一步了。下面是我用来计算图像熵的方法:
import Image
from math import log
def get_histogram_dispersion(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)
im = Image.open('test.png')
h = im.histogram()
print get_histogram_dispersion(h)