RGB与HEX颜色代码的转换指南
最编程
2024-01-13 07:56:11
...
def convert(value):
"""
convert 2------>16
:param value: input int type
:return: str type(16)
"""
if value == 10:
return "A"
elif value == 11:
return "B"
elif value == 12:
return "C"
elif value == 13:
return "D"
elif value == 14:
return "E"
elif value == 15:
return "F"
return str(value)
def convert_hex(value):
"""
convert 16------2
:param value: input string type
:return: str type(2)
"""
if value == "A":
return str(10)
elif value == "B":
return str(11)
elif value == "C":
return str(12)
elif value == "D":
return str(13)
elif value == "E":
return str(14)
elif value == "F":
return str(15)
return str(value)
def rgb2hex(rgb):
"""
Convert RGB to HEX color
:param rgb: Rge value example(23,32,44)
:return: Hex value example #??????
"""
hex = []
for i in rgb:
if i == 0:
h = str(0) + str(0)
else:
h_left = i / 16
h_right = i % 16
h = convert(h_left) + convert(h_right)
hex.append(h)
hex_combine = "#" + ''.join(hex)
return hex_combine
def hex2rgb(hex_value):
"""
Convert hex to rgp
:param hex_value: string type example "#1722DF"
:return: a rgb color tuple example (23,34,223)
"""
hex = hex_value[1:]
hex_splite = [convert_hex(x) for x in hex if x]
hex_splite_rgb = splite_string(hex_splite, 2)
rgb = [int(line[0]) * 16 + int(line[1]) for line in hex_splite_rgb if line]
return tuple(rgb)
def splite_string(s, n):
return [s[i:i + n] for i in range(len(s)) if i % n == 0]
"""
convert 2------>16
:param value: input int type
:return: str type(16)
"""
if value == 10:
return "A"
elif value == 11:
return "B"
elif value == 12:
return "C"
elif value == 13:
return "D"
elif value == 14:
return "E"
elif value == 15:
return "F"
return str(value)
def convert_hex(value):
"""
convert 16------2
:param value: input string type
:return: str type(2)
"""
if value == "A":
return str(10)
elif value == "B":
return str(11)
elif value == "C":
return str(12)
elif value == "D":
return str(13)
elif value == "E":
return str(14)
elif value == "F":
return str(15)
return str(value)
def rgb2hex(rgb):
"""
Convert RGB to HEX color
:param rgb: Rge value example(23,32,44)
:return: Hex value example #??????
"""
hex = []
for i in rgb:
if i == 0:
h = str(0) + str(0)
else:
h_left = i / 16
h_right = i % 16
h = convert(h_left) + convert(h_right)
hex.append(h)
hex_combine = "#" + ''.join(hex)
return hex_combine
def hex2rgb(hex_value):
"""
Convert hex to rgp
:param hex_value: string type example "#1722DF"
:return: a rgb color tuple example (23,34,223)
"""
hex = hex_value[1:]
hex_splite = [convert_hex(x) for x in hex if x]
hex_splite_rgb = splite_string(hex_splite, 2)
rgb = [int(line[0]) * 16 + int(line[1]) for line in hex_splite_rgb if line]
return tuple(rgb)
def splite_string(s, n):
return [s[i:i + n] for i in range(len(s)) if i % n == 0]
推荐阅读
-
[姿势估计] 实践记录:使用 Dlib 和 mediapipe 进行人脸姿势估计 - 本文重点介绍方法 2):方法 1:基于深度学习的方法:。 基于深度学习的方法:基于深度学习的方法利用深度学习模型,如卷积神经网络(CNN)或递归神经网络(RNN),直接从人脸图像中学习姿势估计。这些方法能够学习更复杂的特征表征,并在大规模数据集上取得优异的性能。方法二:基于二维校准信息估计三维姿态信息(计算机视觉 PnP 问题)。 特征点定位:人脸姿态估计的第一步是通过特征点定位来检测和定位人脸的关键点,如眼睛、鼻子和嘴巴。这些关键点提供了人脸的局部结构信息,可用于后续的姿势估计。 旋转表示:常见的旋转表示方法包括欧拉角和旋转矩阵。欧拉角通过三个旋转角度(通常是俯仰、偏航和滚动)描述头部的旋转姿态。旋转矩阵是一个 3x3 矩阵,表示头部从一个坐标系到另一个坐标系的变换。 三维模型重建:根据特征点的定位结果,三维人脸模型可用于姿势估计。通过将人脸的二维图像映射到三维模型上,可以估算出人脸的旋转和平移信息。这就需要建立人脸的三维模型,然后通过优化方法将模型与特征点对齐,从而获得姿势估计结果。 特征点定位 特征点定位是用于检测人脸关键部位的五官基础部分,还有其他更多的特征点表示方法,大家可以参考我上一篇文章中介绍的特征点检测方案实践:人脸校正二次定位操作来解决人脸校正的问题,客户在检测关键点的代码上略有修改,坐标转换部分客户见上图 def get_face_info(image). img_copy = image.copy image.flags.writeable = False image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = face_detection.process(image) # 在图像上绘制人脸检测注释。 image.flags.writeable = True image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) box_info, facial = None, None if results.detections: for detection in results. for detection in results.detections: mp_drawing.Drawing.detection = 无 mp_drawing.draw_detection(image, detection) 面部 = detection.location_data.relative_keypoints 返回面部 在上述代码中,返回的数据是五官(6 个关键点的坐标),这是用 mediapipe 库实现的,下面我们可以尝试用另一个库:dlib 来实现。 使用 dlib 使用 Dlib 库在 Python 中实现人脸关键点检测的步骤如下: 确保已安装 Dlib 库,可使用以下命令: pip install dlib 导入必要的库: 加载 Dlib 的人脸检测器和关键点检测器模型: 读取图像并将其灰度化: 使用人脸检测器检测图像中的人脸: 对检测到的人脸进行遍历,并使用关键点检测器检测人脸关键点: 显示绘制了关键点的图像: 以下代码将参数 landmarks_part 添加到要返回的关键点坐标中。
-
深入理解各类数值转换:二进制、八进制、十进制与十六进制的详尽转换教程 - 序章:整数转换指南
-
图解指南:二进制、八进制与十六进制相互转换详解——(四) 八进制到十六进制的转化实例
-
理解LDO与DC-DC转换器的区别及选型指南
-
在 iOS 中,如何轻松让显示的货币与本地货币转换?苹果系统下的货币换算步骤指南
-
如何在JavaScript中进行rem与px的转换:从/rem到px的实践指南
-
在Java中,如何快速实现XML到JSON的转换? - 解析与生成指南:关键步骤包括解析XML、构造数据模型、转化为JSON。讲解如下:首先,利用JDOM库读懂XML文档,提取元素和属性;接着,基于这些信息设计适当的数据结构(如树形或网状结构);最终,运用Jackson库将数据结构顺利转存为JSON格式。同时,我们也讨论了何时选用JDOM和Jackson这类工具辅助转换过程。
-
转换技巧:在Seurat、SingleCellExperiment与scanpy对象之间穿梭 - 1. 单细胞实验对象间的互转指南
-
ASCII码对照表:字母与数字符号的转换指南
-
ASCII码值详解:大写小写字母与数字的基本编码,以及字母与数字之间的转换指南