python pil 提取图片主要颜色

from PIL import Image, ImageDraw, ImageFont
from pylab import *
import libcc

def rgb2hsv(r, g, b):
    r, g, b = r/255.0, g/255.0, b/255.0
    mx = max(r, g, b)
    mn = min(r, g, b)
    m = mx-mn
    if mx == mn:
        h = 0
    elif mx == r:
        if g >= b:
            h = ((g-b)/m)*60
        else:
            h = ((g-b)/m)*60 + 360
    elif mx == g:
        h = ((b-r)/m)*60 + 120
    elif mx == b:
        h = ((r-g)/m)*60 + 240
    if mx == 0:
        s = 0
    else:
        s = m/mx
    v = mx
    H = h / 2
    S = s * 255.0
    V = v * 255.0
    return H, S, V

def yellow(H, S, V):
    if(H>=26 and H<=34 and S>=43 and S<=255 and V>=46 and V<=255):
        return True

def green(H, S, V):
    if(H>=35 and H<=77 and S>=43 and S<=255 and V>=46 and V<=255):
        return True
    else:
        return False
    


def get_dominant_colors(infile):
    image = Image.open(infile)
    
    # 缩小图片,否则计算机压力太大
    # small_image = image.resize((80, 80))
    # result = small_image.convert(
    result = image.convert(
        "P", palette=Image.ADAPTIVE, colors=10
    )  
	
	# 10个主要颜色的图像

    # 找到主要的颜色
    palette = result.getpalette()
    color_counts = sorted(result.getcolors(), reverse=True)
    colors = list()
    maincount=10

    for i in range(maincount):
        palette_index = color_counts[i][1]
        dominant_color = palette[palette_index * 3 : palette_index * 3 + 3]
        colors.append(tuple(dominant_color))

    ax2 = plt.subplot(211)
    img = Image.new('RGBA', image.size, (255, 255, 255))
    print("\n============ the image size ===============")
    print (img.size)
    # 图片显示的x y轴
    y = img.size[1]  # y坐标  x*y = 行数
    w = int(img.size[0] / maincount)
    
    draw = ImageDraw.Draw(img)
    word_size = 50 #文字大小
    word_css  = "C:\\Windows\\Fonts\\STXINGKA.TTF" #字体文件   行楷
    font = ImageFont.truetype(word_css,111)

    for i in range(0, maincount):
        for j in range(i * w, (i + 1) * w):
            for k in range(0, y):
                rgb = colors[i]
                img.putpixel((j, k), (int(rgb[0]), int(rgb[1]), int(rgb[2])))  # rgb转化为像素
        draw.text((i * w, 0), str(i),(255-colors[i][0],255-colors[i][1],255-colors[i][2]), font = font )
    plt.xlabel(u'color')
    plt.title(u'main color sort')
    plt.yticks()
    plt.imshow(img)
    plt.tight_layout()

    # 显示原图,也就是要处理的图像
    plt.subplot(223)
    plt.title(u'origin image')
    plt.imshow(image)
    plt.subplot(224)
    plt.title(u'result image')
    plt.imshow(result)
    # 显示整个figure
    print(colors)
    for i in colors:
        
        hsvi=rgb2hsv(i[0],i[1],i[2])
        print(colors.index(i), i,'hsv:', hsvi)
        # print( libcc.min_color_diff( i))
        print('green:',green(hsvi[0], hsvi[1], hsvi[2]))
    plt.show()
     # print(colors)
    return colors

image_path = r"2.jpg"
color = get_dominant_colors(image_path)
print(color)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值