Skip to main content
 首页 » 编程设计

python之为什么 python-colormath 不能将 sRGB 转换为 Adobe RGB

2025年04月02日15langtianya

这似乎是一个如此简单的问题,正是 colormath 设计的目的。但是调用 convert_color 似乎返回传入的相同对象。根据 the documentation失败的转换应该引发 UndefinedConversionError,而不是返回对象。

>>> from colormath.color_objects import sRGBColor, AdobeRGBColor 
>>> from colormath.color_conversions import convert_color 
>>> srgb = sRGBColor(0.0, 1.0, 0.0) 
>>> srgb 
sRGBColor(rgb_r=0.0,rgb_g=1.0,rgb_b=0.0) 
>>> argb = convert_color(srgb, AdobeRGBColor) 
>>> argb 
sRGBColor(rgb_r=0.0,rgb_g=1.0,rgb_b=0.0) 
>>> argb is srgb 
True 

它确实可以转换为 Lab,所以我不确定问题出在哪里。

>>> from colormath.color_objects import LabColor 
>>> convert_color(srgb, LabColor) 
LabColor(lab_l=87.73500278716472,lab_a=-86.1829494051608,lab_b=83.1795364492565) 

请您参考如下方法:

convert_color 中conversion 变量的内容使用您的示例的定义是一个空列表,这意味着没有要执行的转换,因此定义不会失败并返回 new_color 这是用您的原始 sRGB 颜色初始化的。不过,我不确定为什么会这样。

或者,我是另一个 Python 的维护者 Colour Science适用于您的情况的 API,但是它可能比 colormath 涉及更多,因为它没有抽象转换:

import colour 
 
colour.RGB_to_RGB( 
  (0, 1, 0),  
  colour.models.sRGB_COLOURSPACE,  
  colour.models.ADOBE_RGB_1998_COLOURSPACE) 
 
 
# array([ 0.28488056,  1.        ,  0.04116936])