Converting colors (not images) with ImageMagick

后端 未结 2 1538
北荒
北荒 2021-01-27 10:47

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)

相关标签:
2条回答
  • 2021-01-27 11:44

    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%)
    
    0 讨论(0)
  • 2021-01-27 11:45

    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%)
    
    0 讨论(0)
提交回复
热议问题