ImageMagick如何输出十六进制颜色而不是SRGB



在这个问题中,有人问如何在十六进制符号中获得图像的平均颜色。经过一番研究,我发现了使用ImageMagick的一种有效的解决方案:

user@laptop:~$ convert rose: -scale 1x1! -format '%[pixel:s]n' info:-

问题是它打印srgb(146,89,80)而不是所需的#925950

我尝试阅读-format的文档,该文档确实提到了%[hex:]" Thing",但是当将%[pixel:s]替换为%[hex:s]时,我会收到以下错误:

convert: unknown image property "%[hex:s]" @ warning/property.c/InterpretImageProperties/3678.

我也尝试阅读FX Expressions的文档,但是我不知道如何输出结果作为十六进制代码而不是SRGB。

您很可能会出现错误,因为您的ImageMagick版本太老了。Changelog说:

2017-06-02 6.9.8-9 Cristy <quetzlzacatenango@image...>
Add support for 'hex:' property.

如果该版本或以后使用:

convert rose: -scale 1x1! -format "%[hex:u]n" info:
925950
convert rose: -scale 1x1! -format "%[hex:s]n" info:
925950
convert rose: -scale 1x1! -format "%[hex:u.p{0,0}]n" info:
925950
convert rose: -scale 1x1! -format "#%[hex:u]n" info:
#925950
convert rose: -scale 1x1! -format "#%[hex:s]n" info:
#925950
convert rose: -scale 1x1! -format "#%[hex:u.p{0,0}]n" info:
#925950

如果较早,则

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

在询问有关ImageMagick命令的问题时,最好提供您的ImageMagick版本和平台,因为语法可能会有所不同,并且可以添加新功能或修复了错误。

附录:

| awk -F '[(,)]' '{printf("#%x%x%xn",$2,$3,$4)}'

输出:

#925950

最新更新