Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick: How can i work with histogram result?

Tags:

imagemagick

im getting the most existing colors of an image and display it with the "histogram" funktion like

convert image.jpg  -scale 100x100 -gravity \
center -crop 50% -dither None -remap color_map.gif \
-format %c histogram:info:

    22: (  0,  0,  0) #000000 black
   881: (119,133,142) #77858E rgb(119,133,142)
   268: (186, 84, 29) #BA541D rgb(186,84,29)
   662: (212,212,212) #D4D4D4 grey83
    20: (244,203, 98) #F4CB62 rgb(244,203,98)
   647: (255,255,255) #FFFFFF white

Ho can i work now with this output? i want to save the most existing color in my database, but i dont know how to get now only the color with the number 881. Can any one help me?

like image 729
Alexandr Avatar asked Sep 14 '25 10:09

Alexandr


1 Answers

If you want to do this purely from the shell (assuming Bash-like Unix environment), something like this would work:

convert image.jpg -scale 100x100 -gravity center \
  -crop 50% -dither None -remap color_map.gif \
  -format %c histogram:info: | sort | tail -n1 | \
  sed -e 's/.*\(#[0-9A-F]\+\).*/\1/'

That takes the output from ImageMagick, sorts it so the largest color count is on the bottom, takes just that line, then extracts the color hex from the line. You can tweak the sed regex if your goal is to get the decimal rgb values instead of the hex.

So for your example image, the output should be just:

#77858E
like image 107
pendor Avatar answered Sep 17 '25 20:09

pendor