Fred's ImageMagick Scripts



    Licensing:

    My scripts are available free of charge for non-commercial use.

    If you redistribute or incorporate any of these scripts into other free applications, you may use my scripts by simply referencing my name and this web page: Fred Weinhaus and http://www.fmwconcepts.com/imagemagick/index.html

    For use of my scripts in commercial use or non-free applications, please contact me for licensing arrangements.
    My email address is fmw at alink dot net.

    Usage, whether stated in script or not, is also subject to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

SPECTRUMHIST


Creates either a spectrum or histogram of the colors in an image.

Download Script

last modified: October 06, 2011



USAGE: spectrumhist [-t type] [-s sort] [-w width] [-h height] [-m magnify] [-c colors] infile outfile
USAGE: spectrumhist [-help]

-t ... type ...... type of output; histogram (h) or spectrum (s);
.................. default=histogram
-s ... sort ...... sort mode; count (c), hue (h), sat (s), lum (l),
.................. rgb (r) or bgr (b); default=hue
-w ... width ..... width of bars in bar chart; integer>0; default=1
-h ... height .... height of output image; integer>0; default=100
-m ... magnify ... magnification for count scaling; integer>0; default=1
-c ... colors .... color reduction for input; 0<integer<=256; default=256

PURPOSE: To create either a spectrum or histogram of the colors in an image.

DESCRIPTION: SPECTRUMHIST creates either a color spectrum or histogram of the colors in an image. This is a bar graph of the exact colors rather than a histogram by channels. The graph may be sorted either by count or by hue.

ARGUMENTS:

-t type ... TYPE of output. Choices are histogram (h) or spectrum (s). The default=histogram

-s sort ... SORT is the mode of sorting the colors. The choices are: count (c), hue (h), sat (s) for saturation, lum (l) for luminosity (i.e. intensity), rgb (r) or bgr (b). The default=hue

-w width ... WIDTH of the bars in the bar chart. Values are integers>0. The default=1

-h height ... HEIGHT of the output image. Values are integer>0. The default=100

-m magnify ... MAGNIFY is the magnification factor for count scaling. This scales the height of each bar of the histogram, but does not affect the height of the image. Any bar that is taller than the image will be clipped. This is not relevant to type=spectrum. Values are integers>0. The default=1

-c colors ... COLORS is the number of colors desired for color reduction in the input image. Values are 0<integer<=256. The default=256. If the image has fewer colors than selected, the actual number of colors will be used.

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES

Example 1

Original

Arguments:
-t histogram -s hue -w 10

Arguments:
-t histogram -s count -w 10

Arguments:
-t spectrum -s hue -w 10

Arguments:
-t spectrum -s count -w 10



Example 2

Original
(http://www.entheosweb.com/photoshop/glass_effects.asp)

Arguments:
-t histogram -s hue

Arguments:
-t histogram -s count

Arguments:
-t spectrum -s hue

Arguments:
-t spectrum -s count

Arguments:
-t histogram -s hue -w 10 -c 24

Arguments:
-t histogram -s count -w 10 -c 24

Arguments:
-t spectrum -s hue -w 10 -c 24

Arguments:
-t spectrum -s count -w 10 -c 24



Example 3

Original
( http://www.thecartoonpictures.com/r-cartoon-animal-224-cartoon-parrot-1465.htm)

Arguments:
-t histogram -s hue -m 5

Arguments:
-t histogram -s count -m 5

Arguments:
-t spectrum -s hue

Arguments:
-t spectrum -s count

Arguments:
-t histogram -s hue -w 10 -c 12

Arguments:
-t histogram -s count -w 10 -c 12

Arguments:
-t spectrum -s hue -w 10 -c 12

Arguments:
-t spectrum -s count -w 10 -c 12



Example 4

Original

Arguments:
-t histogram -s hue -m 100

Arguments:
-t histogram -s count -m 100

Arguments:
-t spectrum -s hue

Arguments:
-t spectrum -s count

Arguments:
-t histogram -s hue -w 10 -c 12

Arguments:
-t histogram -s count -w 10 -c 12

Arguments:
-t histogram -s hue -w 10 -c 12 -m 5

Arguments:
-t histogram -s count -w 10 -c 12 -m 5



Example 5 -- Spectrum vs Sort

Original
( http://www.thecartoonpictures.com/r-cartoon-animal-224-cartoon-parrot-1465.htm)

Arguments:
-t spectrum -s count

Arguments:
-t spectrum -s hue

Arguments:
-t spectrum -s sat

Arguments:
-t spectrum -s lum

Arguments:
-t spectrum -s rgb

Arguments:
-t spectrum -s bgr



Example 6 -- Spectrum vs Sort

Original

Arguments:
-t spectrum -s count

Arguments:
-t spectrum -s hue

Arguments:
-t spectrum -s sat

Arguments:
-t spectrum -s lum

Arguments:
-t spectrum -s rgb

Arguments:
-t spectrum -s bgr



What the script does is as follows for the case of type=histogram and sort=hue:

  • Converts image to the desired number of colors and HSL color space
  • Generates the histogram in text format and filters the counts and colors
    and sorts by increasing hue
  • For each row of the histogram count and colors, appends the
    -size ${width}x${ht} xc:hsl(hue,sat,bri) to a list, where
    ht=(magnify*100*count/maxcount
  • Applies the list to a command to create each bar as an image
    and append them all together

This is equivalent to the following IM commands

  • convert -quiet -regard-warnings "$infile" -alpha off +dither -colors $colors -depth 8 +repage "$tmpA1"
  • ww=$width
  • hh=$height
  • string=`convert $tmpA1 -colorspace HSL -format %c histogram:info:- |\
    sed -n 's/^ *\(.*\):.*hsl(\(.*\))$/\1 \2/p' | sort -n -k 2 |\
    awk -v wd="$width" -v mag="$magnify" '
    { list=""; i=NR; count[i]=$1; color[i]=$2; if (count[i]>maxcount) maxcount=count[i]; }
    END { for (i=1; i<=NR; i++)
    { ht[i]=int(mag*100*count[i]/maxcount+0.5); list=(list "\ " "-size\ " wd"x"ht[i] "\ " "xc:hsl("color[i])")" }
    {print list; } } '`
  • convert -size ${ww}x${hh} $string -background black +append -flip \
    -gravity south -crop ${www}x${hh}+0+0 +repage $outfile