當前位置:首頁 » 操作系統 » 顏色識別演算法

顏色識別演算法

發布時間: 2022-12-13 16:08:26

① 用python寫識別圖片主要顏色的程序

#-*-coding:utf-8-*-

importcolorsys

defget_dominant_color(image):

#顏色模式轉換,以便輸出rgb顏色值
image=image.convert('RGBA')

#生成縮略圖,減少計算量,減小cpu壓力
image.thumbnail((200,200))

max_score=None
dominant_color=None

forcount,(r,g,b,a)inimage.getcolors(image.size[0]*image.size[1]):
#跳過純黑色
ifa==0:
continue

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)

#忽略高亮色
ify>0.9:
continue

#Calculatethescore,.
#Add0.1tothesaturationsowedon'tcompletelyignoregrayscale
#,butstillgivethemalow
#weight.
score=(saturation+0.1)*count

ifscore>max_score:
max_score=score
dominant_color=(r,g,b)

returndominant_color

if__name__=="__main__":
fromPILimportImage
importos

path=r'.\pics\'
fp=open('file_color.txt','w')
forfilenameinos.listdir(path):
printpath+filename
try:
color=get_dominant_color(Image.open(path+filename))
fp.write('Thecolorof'+filename+'is'+str(color)+' ')
except:
print"Thisfileformatisnotsupport"
fp.close()


pics文件夾和python程序在一個目錄下,產生的文件名file_color.txt也在這個目錄下。

看看能否幫到你

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:569
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:857
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:556
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:738
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:659
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:980
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:231
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:89
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:781
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:685