2017-08-17 144 views
0

我使用JS ImageMagick的,我也得到一個像素與他的座標的顏色是這樣的:將grey60轉換爲rgb | ImageMagick的

im.identify(['-format', '%[pixel:p{' + values.coordinate + '}]', values.file], function(err, color) { 
    console.log('color = ', color); 
}); 

它的工作原理,但有時,我得到了像「grey60」或「grey40」什麼顏色像那樣。 有沒有一種方法可以精確到imagemagick在hexa或rgb中有數據?或者有辦法將這種格式轉換爲hexa或rgb?

+1

某個功能在7.0.5版本左右的某個時間添加到IM中,可讓您訪問像素的十六進制值。它看起來像這樣...「%[hex:u.p {W,H}]」。它不包含輸出中的「#」,所以如果你需要的話,你將不得不構建你的字符串來包含它。 – GeeMack

回答

1

在IM 6.9.8-9和7.0.5.10中,對%[hex:]屬性添加了支持,類似於%[pixel:],但返回了十六進制值。所以這應該在命令行模式下工作。

convert xc:red -depth 8 -format "%[hex:u.p{0,0}]\n" info: 
FF0000 

或添加#符號:

convert xc:red -depth 8 -format "\#%[hex:u.p{0,0}]\n" info: 
#FF0000 

對於IM的版本之前(至少對Unix語法):

convert xc:red -depth 8 txt: | tail -n +2 | sed -n 's/^.*\(\#.*\) .*$/\1/p' 
#FF0000 

所以,如果你想在一個十六進制顏色座標,做類似的事情:

convert rose: -depth 8 -format "\#%[hex:u.p{20,20}]\n" info: 
#A93B2A 

convert rose:[1x1+20+20] txt: | tail -n +2 | sed -n 's/^.*\(\#.*\) .*$/\1/p' 
#A93B2A