Fred's ImageMagick Scripts


 

 

COLORSPECTRUM


Generates a spectrum-like image from the colors in an image

Download Script

last modified: May 30, 2008



USAGE: colorspectrum [-c columns] [-r rows] [-l] infile outfile
USAGE: colorspectrum [-h or -help]

-c ...... columns .... width of spectrum; determines the maximum
...................... number of colors; 1<=columns<=255; default=255
-r ...... rows ....... height of spectrum; rows>=1; default=50
-l ................... displays a list of colors to the terminal

PURPOSE: To generate a spectrum-like image from the colors in an image.

DESCRIPTION: PROFILE generates generate a spectrum-like image from the colors in an image. It uses a color reduction algorithm to extract the specified number of colors. Then it sorts the colors according to hue and creates an image of the specified height and width. See http://www.imagemagick.org/script/quantize.php for a description of the color reduction algorithm.

ARGUMENTS:

-c columns ... COLUMNS is the width of the spectrum and also determines the number of colors to try to generate. Allowed values are integers between 1 and 255. Note that it is possible that the color reduction algorithm will produce fewer, but never more colors than desired. If fewer colors are generated, then they will show as black areas on the left side of the spectrum. The default is 255.

-r rows ... ROWS is the height of the spectrum. Values are integers greater than 0. The default is 100.

-l ... Indicates to print a list of colors to the terminal.

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


Image And Spectrum

Original Image

Spectrum Image
(default arguments)



What the script does is as follows:

  • Extracts the desired number of colors from an image as text
  • Converts the text colors to a single row image in HSL colorspace
  • Converts the HSL single row image to text and sorts the colors
    according to hue
  • Converts the sorted text colors to a single row image in RGB
    colorspace and scales to the desired height

This is equivalent to the following IM commands:

  • spectrum1=`convert $infile +matte +dither -colors $columns -unique-colors txt:- | \
    sed -n 's/ //g; s/[0-9]*,[0-9]*:(\([0-9]*,[0-9]*,[0-9]*\).*/\1/; s/,/ /g; 2,$p'`
  • echo "P3 $columns 1 $qmax\n $spectrum1" | convert - -colorspace HSL $tmp0
  • spectrum2=`convert $tmp0 txt:- | \
    sed -n 's/ //g; s/[0-9]*,[0-9]*:(\([0-9]*,[0-9]*,[0-9]*\).*/\1/; s/,/ /g; 2,$p' | \
    sort -n -k 1,1`
  • echo "P3 $columns 1 $qmax\n $spectrum2" | convert - $tmp0
  • convert $tmp0 -colorspace HSL \
    \( $tmp0 -channel Red -separate \) -compose CopyRed -composite \
    \( $tmp0 -channel Green -separate \) -compose CopyGreen -composite \
    \( $tmp0 -channel Blue -separate \) -compose CopyBlue -composite \
    -colorspace RGB -scale ${columns}x${rows}! $outfile