使用PIL和Python2.7。
我有下面的代码来判断图像是暗的还是光的。
def blackWhite(image):
'''Return `True` if the image is mostly white, else `False`'''
l=image.convert('L').load()
w,h=image.size
lums=sum([[l[x,y] for x in range(w)] for y in range(h)],[])
return sum(lums)/float(len(lums))>127我试图修改这一点,使指向图像中心的点的重量比外部的更重。
到目前为止,我已经编写了以下代码:
def blackWhite(image):
'''Return `True` if the image is mostly white, else `False` Weigh center pixels more heavily.'''
def weight(x,y):
'''Return the weight of the point at (x,y) based on function y = -0.5|x-8|+4'''
return (-0.5*abs(x-8)+4)*(-0.5*abs(x-8)+4)
l=image.convert('L').load()
w,h=image.size
lums=sum([[l[x,y] for x in range(w)] for y in range(h)],[])
weights=sum([[weight(x,y) for x in range(w)] for y in range(h)],[])
weighted=[x*weights[n] for n,x in enumerate(lums)]
return (sum(weighted)/float(len(weighted)))/sum(weights)这个返回的值不是我所期望的,我期望255中的值。这段代码中的错误在哪里?
发布于 2015-11-01 22:01:29
对不起,我没有正确的配方。最后一行不应除以加权长度,然后除以权重之和,只应除以权重之和。
return sum(weighted)/sum(weights)https://stackoverflow.com/questions/33466426
复制相似问题