栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现HSV与RGB的互相转换

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现HSV与RGB的互相转换

def RGB2HSV(R,G,B):
    V=[]
    S=[]
    H=[]
    for i in range(len(R)):
        V.append([])
        S.append([])
        H.append([])
        for j in range(len(R[i])):
            max_rgb=max(R[i][j],B[i][j],G[i][j])
            if max_rgb==0:
                V[i].append(0)
                S[i].append(0)
                H[i].append(0)
                continue
            else:
                pass
            min_rgb=min(R[i][j],B[i][j],G[i][j])
            V[i].append(max_rgb)
            S[i].append((max_rgb-min_rgb)/max_rgb)
            if max_rgb==min_rgb:
                H[i].append(0)
                continue
            else:
                pass
            if R[i][j]==max_rgb:
                temp_H=round((G[i][j]-B[i][j])/(max_rgb-min_rgb)*60)
            elif G[i][j]==max_rgb:
                temp_H=round(120+(B[i][j]-R[i][j])/(max_rgb-min_rgb)*60)
            elif B[i][j]==max_rgb:
                temp_H=round(240+(R[i][j]-G[i][j])/(max_rgb-min_rgb)*60)
            while temp_H<0:
                temp_H=temp_H+360
            H[i].append(temp_H)
    return H,S,V
    
def HSV2RGB(H,S,V):
    R2=[]
    G2=[]
    B2=[]
    for i in range(len(H)):
        R2.append([])
        G2.append([])
        B2.append([])
        for j in range(len(H[i])):
            if S[i][j]==0:
                temp_R2=V[i][j]
                temp_G2=V[i][j]
                temp_B2=V[i][j]
            else:
                temp_H=H[i][j]/60
                case=round(temp_H)
                f=temp_H-i
                a=round(V[i][j]*(1-S[i][j]))
                b=round(V[i][j]*(1-S[i][j]*f))
                c=round(V[i][j]*(1-S[i][j]*(1-f)))
                if case==0:
                    temp_R2=V[i][j]
                    temp_G2=c
                    temp_B2=a
                elif case==1:
                    temp_R2=b
                    temp_G2=V[i][j]
                    temp_B2=a
                elif case==2:
                    temp_R2=a
                    temp_G2=V[i][j]
                    temp_B2=c
                elif case==3:
                    temp_R2=a
                    temp_G2=b
                    temp_B2=V[i][j]
                elif case==4:
                    temp_R2=c
                    temp_G2=a
                    temp_B2=V[i][j]
                elif case==5:
                    temp_R2=V[i][j]
                    temp_G2=a
                    temp_B2=b
            while temp_R2 not in range(256):
                if temp_R2<0:
                    temp_R2=temp_R2+255
                else:
                    temp_R2=temp_R2-255
            R2[i].append(temp_R2)
            while temp_G2 not in range(256):
                if temp_G2<0:
                    temp_G2=temp_G2+255
                else:
                    temp_G2=temp_G2-255
            G2[i].append(temp_G2)
            while temp_B2 not in range(256):
                if temp_B2<0:
                    temp_B2=temp_B2+255
                else:
                    temp_B2=temp_B2-255
            B2[i].append(temp_B2)
    return R2,G2,B2
'''
R=[[  0, 10, 50, 70, 90,110,130,150,170,190,210,225],
   [  5, 15, 55, 75, 95,115,135,155,175,195,215,225],
   [ 10, 20, 60, 80,100,120,140,160,180,200,220,225],
   [ 15, 25, 65, 85,105,125,145,165,185,205,225,225],
   [ 20, 30, 70, 90,110,130,150,170,190,210,225,225]]
G=[[225,210,190,170,150,130,110, 90, 70, 50, 10,  0],
   [225,215,295,175,155,135,115, 95, 75, 55, 15,  5],
   [225,220,200,180,160,140,120,100, 80, 60, 20, 10],
   [225,225,205,185,165,145,125,105, 85, 65, 25, 15],
   [225,225,210,190,170,150,130,110, 90, 70, 30, 20]]
B=[]
for i in range(len(R)):
    B.append([])
    for j in range(len(R[i])):
        if j%2==0:
            B[i].append(R[i][j])
        else:
            B[i].append(G[i][j])
'''
R=[[0,255,255,0,0,255,0,255,192,128,128,128,0,128,0,0]]
G=[[0,255,0,255,0,255,255,0,192,128,0,128,128,0,128,0]]
B=[[0,255,0,0,255,0,255,255,192,128,0,0,0,128,128,128]]
H,S,V=RGB2HSV(R,G,B)
R2,G2,B2=HSV2RGB(H,S,V)

for i in range(len(R)):
    for j in range(len(R[i])):
        print('RGB=n')
        print(R[i][j],G[i][j],B[i][j],'n')
        print('HSV=n')
        print(H[i][j],S[i][j],V[i][j],'n')
        print('RGB2=n')
        print(R2[i][j],G2[i][j],B2[i][j],'n')



        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339548.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号