Fred's ImageMagick Scripts


    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial 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:


Computes the spectrum image from the magnitude of the Fourier Transform of an image.

Download Script

last modified: July 28, 2015

USAGE: spectrum [-m] [-s scale] [-g] infile outfile
USAGE: spectrum [-h or -help]

-m ................ generate magnitude from Fourier Transform
................... of the input
-s .... scale ..... scaling constant; integer greater than zero;
................... default will be automatically computed.
-g ................ convert spectrum to grayscale

PURPOSE: To compute the spectrum image from the magnitude of the Fourier Transform of an image.

DESCRIPTION: SPECTRUM computes a spectrum image from the magnitude image generated from the Fourier Transform of an image. The spectrum is basically a scaled log of the magnitude image. See -evaluation log. The log and scaling are provided to emphasize the lower values in the magnitude image relative to the larger values. The input may be the magnitude of the Fourier Transform, if already available. If the -m option is provided, it means that the magnitude of the Fourier Transforms should be computed from the spatial domain input image.


-m ... Compute magnitude of the Fourier Transform of the spatial domain image that is the input. The default, when left off, is to assume that the input image is already the magnitude of the Fourier Transform.

-s scale ... SCALE is the scaling constant used with the log. Values are integers greater than zero. Values are convert to 10^scale for use with log. Typical values range from 2 to 5. Larger values will bring out more low amplitude detail. But too large a value will bring out excessive noise. The default is to compute a scaling value automatically.

-g ... Convert magnitude input to grayscale to produce a grayscale spectrum.

As an final step, pure black (graylevel=0) is converted to graylevel 1 on a scale of 0 to 255. This is done so that after the spectrum is masked with pure black to eliminate noise or apply a filter, the result can be thresholded at value=0 (see -threshold 0) to convert it to a binary mask image.

REQUIREMENTS: IM IM 6.3.9-1 or higher due to the use of -format "%[min]" and "%[max]". Also requires FFTW delegate library to compute the Fourier Transform. Q8 IM compilation is not generally recommended and may not carry enough precision for the Fourier Transform.

See Fourier Transform with ImageMagick, for more details.

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.










convert -size 15x15 xc:white -gravity center \
-background black -extent 128x128 square15.jpg


-m -s 10

-m -s 100

-m -s 1000

convert -size 128x128 xc:black -fill white \
-draw "circle 64,64 64,70" -alpha off circle6.jpg




What the script does is as follows:

  • Computes magnitude of Fourier Transform of the input,
    if the -m option is provided
  • Computes the min and max statistics and stretches the
    image to full dynamic range
  • Applies a scaled log to the intensity values using either
    a user supplied scaling value or computes it automatically as
    a default
  • Shifts any graylevel 0 to graylevel 1 (in range 0 to 255) in the image.

This is equivalent to the following IM commands for red-blue gradient

  • convert $infile -fft -delete 1 $tmpA
  • min=`convert $tmpA -format "%[min]" info:`
  • max=`convert $tmpA -format "%[max]" info:`
  • convert $tmpA -level ${min},${max} $tmpA
  • scale=`convert $tmpA -format "%[fx:floor(exp(log(mean)/log(0.5)))]" info:`
  • convert $tmpA -evaluate log $scale -fill "rgb(1,1,1)" -opaque black $outfile