# 图片主色调自动识别-基于python

python采用Delta E equations和colormath包来计算图片颜色和基础颜色之间的视觉差异程度。

import os

n=range(1,100)

for filename in os.listdir(r"C:\Users\***\Desktop\media"):

if filename.endswith('png'):

if filename.endswith(n):

doc=open("text.txt","w")

from haishoku.haishoku import Haishoku

png==filename.endswith('png')

dominant = Haishoku.getDominant(png)

print (dominant)

print(dominant,file=doc)

doc.close()

RGB转HSV

import os

for filename in os.listdir(r"C:\Users\谭泽宇\Desktop\haishoku-master"):

doc=open("text.txt",'a')

if filename.endswith('png'):

print (filename)

from haishoku.haishoku import Haishoku

png=filename

dominant = Haishoku.getDominant(png)

print (dominant)

print(png,dominant,file=doc)

doc.close()

r,g,b=dominant

print(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)

df = mx-mn

if mx == mn:

h = 0

elif mx == r:

h = (60 * ((g-b)/df) + 360) % 360

elif mx == g:

h = (60 * ((b-r)/df) + 120) % 360

elif mx == b:

h = (60 * ((r-g)/df) + 240) % 360

if mx == 0:

s = 0

else:

s = df/mx

v = mx

print(h,s,v)

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20181028G15SXQ00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2021-12-08

2021-12-08

2021-12-08

2021-12-08

2021-12-08

2021-12-08