我正在尝试根据其亮度质量对图像进行评分,我的代码适用于全彩色图像,但是当我尝试使用透明png时,它会抛出错误说... r,g, b = stat.mean,太多值要解压...,任何提示或提示将不胜感激。
这是我的代码:
def trim(im):
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
def brightness(stat):
r,g,b = stat.mean
brightness = math.sqrt(0.241 * (r ** 2) + 0.691 * (g ** 2) + 0.068 * (b ** 2))
print 'perc brightness %s' % brightness
return 1 - math.fabs(brightness / 100 - 1)
score_points = {
'face': 120,
'sat' : 50,
'sharpness': 100,
'bri': 50,
'con': 100
}
filename = "tmp/thumb_2.png"
im = Image.open(filename)
# crop all black borders
im = trim(im)
# check perceived brightness
v = ImageStat.Stat(im)
b = brightness(v) * score_points['bri']
print 'Brightness score: %s' % b