我在 js 中使用 Imagemagick,我得到了像素的颜色及其坐标,如下所示:
im.identify(['-format', '%[pixel:p{' + values.coordinate + '}]', values.file], function(err, color) {
console.log('color = ', color);
});
它有效,但有时我会得到像“grey60”或“grey40”或类似的颜色。 有没有办法要求 Imagemagick 返回十六进制或 rgb 数据?或者有办法将此格式转换为十六进制或 rgb 吗?
最佳答案
在 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
关于javascript - 将gray60 转换为rgb |图像魔法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45735358/