import colorsys
import PIL.Image as Image
import math
def get_dominant_color(image):
max_score = 0.0001
dominant_color = None
for count, (r, g, b) in image.getcolors(image.size[0] * image.size[1]): #count对应颜色出现次数
# 转为HSV标准
saturation = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)[1]
y = min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235)
y = (y - 16.0) / (235 - 16)
# 忽略高亮色
if y > 0.9:
continue
score = (saturation + 0.1) * count
if score > max_score:
max_score = score
dominant_color = (r, g, b)
return dominant_color
if __name__ == '__main__':
image = Image.open('./zm1.png')
image = image.convert('RGB')
print(get_dominant_color(image))
CV识别图片背景主要颜色
最新推荐文章于 2023-07-14 22:16:24 发布