简体   繁体   English

如何使用Python Imaging Library(PIL)识别非照片或“不感兴趣”的图像

[英]How to identify non-photograph or 'uninteresting' images using Python Imaging Library (PIL)

I have thousands of images and I need to weed out the ones which are not photographs, or otherwise 'interesting'. 我有成千上万的图像,我需要清除那些不是照片或其他“有趣”的图像。

An 'uninteresting' image, for example, may be all one color, or mostly one color, or a simple icon/logo. 例如,“不感兴趣”的图像可以是所有一种颜色,或者主要是一种颜色,或简单的图标/徽标。

The solution doesn't have to be perfect, just good enough to remove the least interesting images. 解决方案不一定非常完美,只需要删除最不有趣的图像就足够了。

My best idea so far is to take a random sampling of pixels, and then... do something with them. 到目前为止,我最好的想法是对像素进行随机抽样,然后......用它们做点什么。

Danphe beat me to it. Danphe打败了我。 Here's my method for calculating image entropy: 这是我计算图像熵的方法:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM