【问题标题】:How can ImageMagick output hex colours instead of SRGB?ImageMagick 如何输出十六进制颜色而不是 SRGB?
【发布时间】:2017-12-26 20:52:01
【问题描述】:

this question,有人问如何以十六进制表示法获取图像的平均颜色。经过一番研究,我找到了一个使用 ImageMagick 的可行解决方案:

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

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

我尝试阅读-format 的文档,其中确实提到了%[hex:]“东西”,但是当用%[pixel:s] 替换%[hex:s] 时,我收到以下错误:

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

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

【问题讨论】:

  • 更近一点但没有十六进制数字:'#%[fx:round(r*256)] %[fx:round(g*256)] %[fx:round(b*256)]\n'
  • 我觉得你需要做:declare $(convert rose: -scale 1x1\! -format "rr=%[fx:round(255*r)]\ngg=%[fx:round(255*g)]\nbb=%[fx:round(255*b)]\n" info:) 然后做:printf "#%02x%02x%02x\n" $rr $gg $bb

标签: bash imagemagick


【解决方案1】:

您遇到错误很可能是因为您的 ImageMagick 版本太旧。变更日志说:

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 版本和平台,因为语法可能会有所不同,并且可能会添加新功能或修复错误。

【讨论】:

  • 也可以缩短:convert rose: -scale 1x1\! txt: | grep -Eo '#[^ ]+'
  • @Cyrus。好的。谢谢。
【解决方案2】:

追加:

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

输出:

#925950

【讨论】:

  • 我希望纯粹在 ImageMagick 中得到答案,但如果 awk 有效,那么就是这样。
猜你喜欢
  • 2017-01-22
  • 2010-12-16
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多