在Python中使用PIL分析图像

时间:2013-03-27 10:30:18

标签: python matplotlib python-imaging-library

我找到了这段代码:

import PIL
from PIL import Image
from matplotlib import pyplot as plt

im = Image.open('./color_gradient.png')  
w, h = im.size  
colors = im.getcolors(w*h)

def hexencode(rgb):
 r=rgb[0]
 g=rgb[1]
 b=rgb[2]
 return '#%02x%02x%02x' % (r,g,b)

for idx, c in enumerate(colors):
    plt.bar(idx, c[0], color=hexencode(c[1]),edgecolor=hexencode(c[1]))
plot.show()

对于确切的链接,可以在此处查看 - Plot image color histogram using matplotlib

我的问题是轴的含义是什么,我如何生成这些值的表?我想运行一些统计数据,比如图片中绿色或红色的百分比......

由于

1 个答案:

答案 0 :(得分:1)

来自PIL Documentation

  

<强> getcolors

     

im.getcolors()=&gt; (计数,颜色)元组列表或无

     

im.getcolors(maxcolors)=&gt; (计数,颜色)元组列表或无

参考图中的Y轴是该颜色的像素数,X轴包含图中的(未分类?)颜色列表。