user7684764
user7684764

Reputation:

Get only hex values from imagemagick

I can get the 10 most frequent colors from an image with this command

convert MYIMAG.JPG  +dither -colors 10 -unique-colors

The output is the following:

# ImageMagick pixel enumeration: 10,1,65535,srgb
0,0: (17797.7,15058.3,10214.1)  #453B28  srgb(69,59,40)
1,0: (26745.1,24530.8,20814.7)  #685F51  srgb(104,95,81)
2,0: (35510.4,30224.2,23717.1)  #8A765C  srgb(138,118,92)
3,0: (33428.3,32608.7,27562.4)  #827F6B  srgb(130,127,107)
4,0: (42221,36875.3,29255.8)  #A48F72  srgb(164,143,114)
5,0: (53896.7,44085.9,24988.3)  #D2AC61  srgb(210,172,97)
6,0: (45384.3,42509,38801.6)  #B1A597  srgb(177,165,151)
7,0: (54519.7,46803.7,37705.7)  #D4B693  srgb(212,182,147)
8,0: (56368.6,48645.3,40350)  #DBBD9D  srgb(219,189,157)
9,0: (58605,50733.4,41256.9)  #E4C5A1  srgb(228,197,161)

Now I would like to either convert that or simply just get it in the following format:

#453B28
#685F51
#8A765C
#827F6B
#A48F72
#D2AC61
#B1A597
#D4B693
#DBBD9D
#E4C5A1

Is there a way to just get the hex values without the Rest ?

Thank you

Upvotes: 1

Views: 991

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207748

If you only have 10 colours. you may be prepared to put up with this awkwardness:

convert -size 10x1 gradient: -depth 8 -format "%[hex:p{0,0}]\n%[hex:p{1,0}]\n%[hex:p{2,0}]\n%[hex:p{3,0}]\n%[hex:p{4,0}]\n%[hex:p{5,0}]\n%[hex:p{6,0}]\n%[hex:p{7,0}]\n%[hex:p{8,0}]\n%[hex:p{9,0}]\n" info:

FFFFFF
E3E3E3
C6C6C6
AAAAAA
8E8E8E
717171
555555
393939
1C1C1C
000000

Other than that, I don't know of a native way to do as you ask, and Jonathon's method is as good as any. Another option may be like this, where I create a little image with a red, green, blue and white pixel then dump it as RGB and use xxd to format it in columns of 3 for R,G,B:

convert xc:red xc:lime xc:blue xc:white +append -depth 8 rgb: | xxd -p -c3
ff0000         # red
00ff00         # green
0000ff         # blue
ffffff         # white

I added the colour names for clarification - they don't actually come out of the process.

Upvotes: 0

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137497

convert ... | tail -n +2 | awk '{ print $3 }'

Use tail to skip the undesired first line of output.

Then use a simple awk program to keep just the 3rd column.

Upvotes: 2

Related Questions