使用 ImageMagick 转换颜色(不是图像)

Converting colors (not images) with ImageMagick

更具体地说,我想准确地将 CMYK 值(可能来自 ISO Coated v2 space)转换为 RGB 值(可能来自 sRGB space) 在 Ruby 平台上(可能使用 ICC 配置文件)。

ImageMagick 似乎是一个不错的起点,但我也听说 LittleCMS 可能 ported/wrapped 可以与 Ruby 一起使用。

再一次,我希望转换单一颜色,而不是图像文件。有什么想法吗?

在 ImageMagick 中,您可以执行以下操作:

convert xc:"cmyk(0,255,255,0)" -colorspace sRGB -format "%[pixel:u.p{0,0}]\n" info:
red

convert xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93%,11%,14%)

Is there anything you can tweak in format to ensure more significant digits in the srgb(X%,X%,X%)

可能是因为 IM 版本不同。 IM 7.0.7.8 显示 srgb(93.0648%,11.1254%,14.1741%)。 IM 6.9.9.20 显示整数。我尝试将 -precision 4 添加到 IM 6 命令行,但仍然得到整数。为了获得更高的精度,必须解析 txt: 输出格式。

例如不解析:

convert xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (60990,7291,9289)  #EE3E1C7B2449  srgb(93%,11%,14%)

所以需要解析括号内的16位值(IM Q16),即(60990,7291,9289)

vals=`convert xc:"cmyk(0,255,255,0)" \
-profile /Users/fred/images/profiles/USWebCoatedSWOP.icc \
-profile /Users/fred/images/profiles/sRGB.icc txt: |\
tail -n +2 | sed -n 's/^.*[(]\(.*\)[)][ ]*\#.*$//p'`
red=`echo $vals | cut -d, -f1`
green=`echo $vals | cut -d, -f2`
blue=`echo $vals | cut -d, -f3`
red=`convert -precision 4 xc: -format "%[fx:100*$red/quantumrange]" info:`
green=`convert -precision 4 xc: -format "%[fx:100*$green/quantumrange]" info:`
blue=`convert -precision 4 xc: -format "%[fx:100*$blue/quantumrange]" info:`
color="srgb($red%,$green%,$blue%)"
echo "$color"
srgb(93.06%,11.13%,14.17%)

调整-precision,为您想要的有效数字位数。

注意:在 IM 7 中,-precision 确实有效。

magick xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93.0648%,11.1254%,14.1741%)

magick -precision 4 xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93.06%,11.13%,14.17%)

magick -precision 2 xc:"cmyk(0,255,255,0)" -profile /Users/fred/images/profiles/USWebCoatedSWOP.icc -profile /Users/fred/images/profiles/sRGB.icc -format "%[pixel:u.p{0,0}]\n" info:
srgb(93%,11%,14%)