Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

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

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

COLORSPECTRUM


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

Download Script

last modified: December 11, 2023



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