美文网首页
如何提取图片主色

如何提取图片主色

作者: 郭彦超 | 来源:发表于2020-12-04 17:13 被阅读0次
import os
import cv2
from PIL import Image
import numpy as np 
from collections import Counter
from matplotlib import pyplot as plt

topN = 5
root = 'd:/data/mini_supervisely/JPEGImages/'

size = 256
rate = 0.15
c_rate = 0.35
area = 25
#color_map = {0:'00', 1:'33', 2:'66', 3:'99', 4:'cc', 5:'ff'} 
color_map = {0:'00', 1:'19', 2:'32', 3:'4b', 4:'64', 5:'7d', 6:'96', 7:'af', 8:'c8', 9:'e1', 10:'ff'} 

def getTopN(img, topN):
    rows, cols, _ = img.shape
    data = [] 
    
    for i in range(rows):
        for j in range(cols): 
            rgb = img[i, j] 
            r = int(color_map[round(rgb[0]/area)], 16)
            g = int(color_map[round(rgb[1]/area)], 16)
            b = int(color_map[round(rgb[2]/area)], 16)
            if r == g and r == b and r in (153, 204, 102, 51):
                data.append((155, 155, 155))
            else:
                data.append((r, g, b))
    return Counter(data).most_common(topN)

def getColorTopN(img, c_img, topN): 
    data = []
    keys = [] 
     
    top = getTopN(c_img, 2) 
     
    top += getTopN(img, topN*20)
      
    for c in range(len(top)):
        tp=top[c][0]
        r = tp[0]
        g = tp[1]
        b = tp[2]
        no_key = True
        for k in keys:
            if (abs(r-k[0]) <= 25 or abs(g-k[1]) <= 25 or abs(b-k[2]) <= 25) and abs(r+b+g - (k[0]+k[1]+k[2])) <= 75 :
                data.append(k)
                no_key = False
                break
        if no_key :
            data.append((r, g, b))
            keys.append((r, g, b))
    top = Counter(data).most_common(topN)
    return top

def detection(path):
    r = 0
    for f in os.listdir(path):
        '''
        if not f == 'FgK0ooKkIKsKIOsQBP8QAhYrOdUQ.png':
            continue
        '''  
        file_path = os.path.join(path, f)
        image = Image.open(file_path)

        width, height = image.size
        print(f)
        height = int(width * size / height)
        width = size

        image = image.resize((height, width),Image.ANTIALIAS)  ## RGB顺序
        img = np.asarray(image) 
        if not len(img.shape)==3:
            continue
        #padding = int(size*rate)
        img2 = img[int(width*rate):int(width*(1-rate)), int(height*rate):int(height*(1-rate))]
        img2 = cv2.GaussianBlur(img2,(7,7),2)
        center_img = img[int(width*c_rate):int(width*(1-c_rate)), int(height*c_rate):int(height*(1-c_rate))]
        #center_img = cv2.GaussianBlur(center_img,(7,7),2)
         
        top = getColorTopN(img2, center_img, topN)
        '''
        center_top = getColorTopN(img[int(width*c_rate):int(width*(1-c_rate)), int(height*c_rate):int(height*(1-c_rate))], topN)
        top += center_top

        top.sort(key= lambda k:-k[1])
        top = top[:topN]
        #print(top)
        '''

        plt.figure(figsize=(60,80))
        #r += 1 
        plt.subplot(1, topN+2, 1)
        plt.imshow(img)
        plt.axis('off')
        #r += 1
        for c in range(len(top)):
            plt.subplot(1, topN+2, c+2)
            img = Image.new('RGB', (10, 10), top[c][0])
            plt.title(top[c][0], fontsize=24)
            plt.imshow(img)
            plt.axis('off')

        plt.subplot(1, topN+2, topN+2) 
        plt.imshow(center_img)
        plt.axis('off')
        r += 1
        plt.show()
        if(r == 100):
            break
            
for cla in os.listdir(r'd:/tmp/颜色标签测试'):
    print(cla)
    detection(os.path.join(r'D:/tmp/颜色标签测试',cla))

效果

相关文章

网友评论

      本文标题:如何提取图片主色

      本文链接:https://www.haomeiwen.com/subject/aweiwktx.html