Fred's ImageMagick Scripts


    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:

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


Interleaves the channels of a single image or each image of a set of multiple images.

Download Script

last modified: December 15, 2018

USAGE: interleave [-t type] [-a alpha] [-c colorize] infile1 ... infileN outfile
USAGE: interleave [-h or -help]

-t .... type ......... type of interleave; choices are: a) row, line, bil or
...................... b) col, pixel, bip; default=row
-a .... alpha ........ include alpha channel; yes or no; default=no
-c .... colorize ..... colorize the channels; yes or no; default=no

PURPOSE: To interleave the channels of a single image or each image of a set of multiple images.

DESCRIPTION: INTERLEAVE interleaves the channels of a single image or each image of a set of multiple images. If a single input image is provided, then each channel will be separated and interleaved either row-by-row or column-by-column. The channels may be recolorized optionally. Images with colorspace RGB or CMYK (with or without alpha channel) are supported. If more than one image is proveded, then each image will be interleaved in succession either row-by-row or column-by-column.


-t type ... TYPE is the type of interleave to use. The choices are either: row (or line or bil) or col (or pixel or bip). BIL and BIP mean band interleaved by line or band interleaved by pixel. The default=row

-a alpha ... ALPHA is a flag to include the alpha channel or not. The choices are: yes or no. The default=no

-c colorize ... COLORIZE is a flag to re-color the channels. It is valid only when one input image is provided. The choices are: yes or no. The default=no.

WARNING: It is not recommended to use JPG for output as the JPG compression will badly distort the output when colorized.

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.



-t row -c no


-t row -c yes


-t col -c no


-t col -c yes

Original 1

Original 2

Original 3

-t row

What the script does is as follows for a 3 channel image for type=row:

  • Creates a striped mask image with one row of white and two rows of black
    that is tiled to the width of the image by 3 times its height
  • Separates the channels of the image and scales them to the same
    width and 3 times its height
  • Creates a transparent image of the same dimensions (3 times the height)
  • Takes the mask inserts it as the alpha channel for the red channel
    and flattens it on the transparent image
  • Rolls the mask by 1 pixel in y and inserts it as the alpha channel
    in green channel and flattens that on the result of the red channel
  • Rolls the mask by 2 pixel in y and inserts it as the alpha channel
    in blue channel and flattens that on the result of the green channel
  • Writes the final result to the output image

This is equivalent to the following IM commands

  • ww=`convert $infile -ping -format "%w" info:`
  • hh=`convert $infile -ping -format "%h" info:`
  • channels=`convert $infile -ping -format "%[channels]" info:`
  • numchannels=`echo "$channels" | wc -m`
  • numchannels=$(($numchannels-1))
  • hhh=`convert xc: -format "%[fx:$numchannels*$hh]" info:`
  • scale=`convert xc: -format "%[fx:$numchannels*100]" info:`
  • convert -size 1x1 $squares -append -write mpr:stripes \
    +delete -size ${ww}x${hhh}! tile:mpr:stripes $dir/tmpM.mpc
  • convert $infile -scale 100x${scale}% -separate +channel $dir/tmpI_%d.mpc
  • convert -size ${ww}x${hhh}! xc:none $dir/tmp0.mpc
  • for ((i=0;i<$numchannels;i++)); do
    convert $dir/tmpM.mpc -roll +0+$i $dir/tmpI_$i.mpc \
    +swap -alpha off -compose copy_opacity -composite -compose over \
    $dir/tmp0.mpc -background none -flatten $dir/tmp0.mpc
  • convert $dir/tmp0.mpc -alpha off $outfile