How can a named configuration color be resolved for a grayscale canvas?

getcolor resolves a color for a requested image mode. The same name can yield a grayscale value or an RGB tuple.

Pixel experiment and interpretation

Compare red in L and RGB before writing to the corresponding canvas. A triple supplied to a single-channel interface has the wrong shape.

from PIL import ImageColor

[ImageColor.getcolor('red',m) for m in ('L','RGB')]

Result [76, (255, 0, 0)]

中文

怎样为灰度画布解析配置文件中的命名颜色?

getcolor 根据目标图像模式解析颜色,同一名称可得到灰度值或 RGB 元组。

像素实验与适用边界

比较 red 在 L 与 RGB 中的结果,再把它写入相应画布。直接把三元组传入单通道接口会让类型不匹配。

from PIL import ImageColor

[ImageColor.getcolor('red',m) for m in ('L','RGB')]

结果 [76, (255, 0, 0)]

References / 参考来源

Keep reading / 继续阅读