如何使用Python Imaging Library(PIL)识别非照片或“无趣"图像 [英] How to identify non-photograph or 'uninteresting' images using Python Imaging Library (PIL)

查看:99
本文介绍了如何使用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)击败了我.这是我计算图像熵的方法:

Danphe beat me to it. 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)

这篇关于如何使用Python Imaging Library(PIL)识别非照片或“无趣"图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆