sambecker
sambecker

Reputation: 1139

Converting colors (not images) with ImageMagick

More specifically, I'd like to accurately convert a CMYK value (probably from the ISO Coated v2 space) to an RGB value (probably from the sRGB space) on the Ruby platform (probably using ICC profiles).

ImageMagick seemed like a good place to start, but I've also heard that LittleCMS might have been ported/wrapped to work with Ruby.

Once again, I'm looking to convert single colors, NOT image files. Any ideas?

Upvotes: 0

Views: 1186

Answers (2)

fmw42
fmw42

Reputation: 53101

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

Likely due to different IM versions. IM 7.0.7.8 shows srgb(93.0648%,11.1254%,14.1741%). IM 6.9.9.20 shows integers. I tried adding -precision 4 to IM 6 command line, but still get integers. To get more precision, one has to parse the txt: output format.

For example without parsing:

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%)

So you need to parse the 16-bit values (for IM Q16) in parenthesis, namely, (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/^.*[(]\(.*\)[)][ ]*\#.*$/\1/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%)

Adjust -precision, for the number of significant digits you want.

NOTE: In IM 7, -precision does work.

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%)

Upvotes: 1

fmw42
fmw42

Reputation: 53101

In ImageMagick, you can do the following:

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%)

Upvotes: 1

Related Questions