2

In this question, someone asked how to get the average colour of an image in hexadecimal notation. After some research, I found a somewhat working solution using ImageMagick:

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

The problem is that this prints srgb(146,89,80) instead of the desired #925950.

I tried reading through the documentation of -format, which did mention a %[hex:] "thing", but when substituting %[pixel:s] for %[hex:s], I get the following error:

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

I also tried reading the documentation for FX Expressions, but I have no idea how I would go about outputting the result as hex code instead of SRGB.

MechMK1
  • 3,278
  • 7
  • 37
  • 55
  • A little closer but no hex numbers: `'#%[fx:round(r*256)] %[fx:round(g*256)] %[fx:round(b*256)]\n'` – Cyrus Dec 26 '17 at 21:40
  • I think you need to do: `declare $(convert rose: -scale 1x1\! -format "rr=%[fx:round(255*r)]\ngg=%[fx:round(255*g)]\nbb=%[fx:round(255*b)]\n" info:)` Then do: `printf "#%02x%02x%02x\n" $rr $gg $bb` – fmw42 Dec 27 '17 at 02:06

2 Answers2

3

You got an error most likely because your version of ImageMagick is too old. The changelog says:

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

If that version or later use:

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

If earlier, then

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 

It is always best to provide your ImageMagick version and platform, when asking questions about ImageMagick commands, since syntax may vary and new feature may be added or bugs fixed.

fmw42
  • 46,825
  • 10
  • 62
  • 80
2

Append:

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

Output:

#925950
Cyrus
  • 84,225
  • 14
  • 89
  • 153