python 32为转16位转RGB用CV展示

代码:

 1 # # -*- coding:utf-8 -*-
 2 # @Time : 2022/5/6
 3 # @Author : 周博
 4 # @File :  num2rgb.py
 5 # @博客园: https://www.cnblogs.com/smartisn/
 6 import cv2
 7 import numpy as np
 8 # #FFFFF_>RGB(255,255,255)
 9 def num2RGB(inputstr):
10     inputstr = "#FFFFFF"
11     str1 = inputstr[1:3]
12     str2 = inputstr[3:5]
13     str3 = inputstr[5:7]
14     num1 = int('0x' + str1, 16)
15     num2 = int('0x' + str2, 16)
16     num3 = int('0x' + str3, 16)
17     return num1,num2,num3
18 # RGB(255,255,255)_>#FFFFF
19 def rgb2NUM(inputstr):
20     list = inputstr.split(',')
21     str1 = list[0][4:]
22     str2 = list[1][1:]
23     str3 = list[2][1:-1]
24     # hex返回的字母A~F均为小写字母a~f,根据样例输出,用upper函数把字母全部转为大写
25     hex1 = hex(int(str1))[2:].upper()
26     hex2 = hex(int(str2))[2:].upper()
27     hex3 = hex(int(str3))[2:].upper()
28     # 十进制转16进制时会出现缺省零的情况,用rjust函数可在字符串左侧填充0
29     # 同理 ljust函数可在字符串的右侧填充0
30     hex1 = hex1.rjust(2, '0')
31     hex2 = hex2.rjust(2, '0')
32     hex3 = hex3.rjust(2, '0')
33     outputstr = "#"
34     outputstr = outputstr + hex1 + hex2 + hex3
35     return outputstr
36 # 展示RGB颜色
37 def ShowRGB(R,G,B):
38     data = np.zeros((400, 400, 3), dtype=np.uint8)
39     data[:] = (R, G, B)
40     cv2.imshow("aaa1", data)
41     cv2.waitKey()
42 if __name__ =="__main__":
43     num_32_10=int("984833",32)
44     num_10_16=hex(num_32_10)
45     R, G, B = num2RGB("#"+str(num_10_16))
46     ShowRGB(R, G, B)
47     print(num_10_16)

 

posted @ 2022-05-06 16:08  博二爷  阅读(198)  评论(0编辑  收藏  举报