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.


Randomly modifies the color of rectangular cells of an image

Download Script

last modified: December 15, 2018

USAGE: colorcells [-n numcells ] [-c cellsize] [-p percent] [-s seeds] [-t thickness] [-d diameters] infile outfile
USAGE: colorcells [-h|help]

-n ... numcells .... number of cells in width and height; comma separated pair of
.................... integers; default="8x8"; used if no cellsize provided
-c ... cellsize .... dimensions of cells; WxH; "x" separated pair of integers;
.................... default is to use numcells; takes precedent over numcells
-p ... percent ..... percent coverage of colored cells; 0<=integer<=100; default=50
-s ... seeds ....... random seeds; comma separate pair of integers; default="200,300"
-t ... thickness ... thickness of white grid lines; integer>0; default=0 (no grid);
.................... odd values will be rounded up to even values
-d ... diameters ... diameters of elliptical mask expressed as percent of width and
.................... height of image; comma separate pair of integers; default="70,70"

PURPOSE: Randomly modifies the color of rectangular cells of an image.

DESCRIPTION: COLORCELLS randomly modifies the color of rectangular cells of an image. The cells may be masked by an ellipse so that the outside of the affected cells are white. Only some percentage of the cells will be modified in color.


-n numcells ... NUMCELLS is the number of cells in width and height. Values are comma separated pair of positive integers. The default="8x8". This option is used if no cellsize are provided.

-c cellsize ... CELLSIZE is the dimensions of the cells expressed as an "x" separated pair of positive integers (WxH). The default is to use numcells. This option takes precedent over numcells.

-p percent ... PERCENT coverage of colored cells. Values are 0<=integer<=100. The \ default=50.

-s seeds ... random number SEEDS. Values are a comma separate pair of positive integers. The default="200,300".

-t thickness ... THICKNESS of white grid lines. Values are integer>=0. The default=0 (no grid). Odd values will be rounded up to even values.

-d diameters ... DIAMETERS of an elliptical mask expressed as a percent of the width and height of the image. Values are a comma separate pair of integers. The default="70,70". If the value is 0 (0,0), then no mask will be used.

NOTE: The script will be slower the more cells that are created.


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.


Example 1

Original Image

-n 8,8 -d 70,70


-n 8,8 -d 100,100


-n 8,8 -d 150,150
-n 8,8 -d 0,0


-n 12,12 -d 80,80


-n 16,16 -d 80,80


-n 8,8 -d 70,70 -t 4

What the script does is as follows:

  • Reads the input image and computes size related arguments
  • Tiles the imaged into cell size parts
  • Optionally creates and tiles an elliptical mask and
    makes white any mask cell that is partially white
  • Combines the tiles and mask tiles and modulates the hue randomly
  • Recombines the tiles into a full image
  • Writes the output

This is equivalent to the following IM commands when no mask is used:

  • WxH=`convert -ping $tmpA1 -format "%wx%h" info:`
  • ww=`echo $WxH | cut -dx -f1`
  • hh=`echo $WxH | cut -dx -f2`
  • coverage=`convert xc: -format "%[fx:(100-$percent)/100]" info:`
  • if [ "$cellsize" != "" ]; then
    cellw=`echo "$cellsize" | cut -dx -f1`
    cellh=`echo "$cellsize" | cut -dx -f2`
    numx=`convert xc: -format "%[fx:floor($ww/$cellw)]" info:`
    numy=`convert xc: -format "%[fx:floor($hh/$cellh)]" info:`
    cropw=`convert xc: -format "%[fx:$numx*$cellw]" info:`
    croph=`convert xc: -format "%[fx:$numy*$cellh]" info:`
    numx=`echo "$numcells" | cut -d, -f1`
    numy=`echo "$numcells" | cut -d, -f2`
    cellw=`convert xc: -format "%[fx:floor($ww/$numx)]" info:`
    cellh=`convert xc: -format "%[fx:floor($hh/$numy)]" info:`
    cropw=`convert xc: -format "%[fx:$numx*floor($ww/$numx)]" info:`
    croph=`convert xc: -format "%[fx:$numy*floor($hh/$numy)]" info:`
  • convert $tmpA1 -crop ${cropw}x${croph} +repage -crop ${cellw}x${cellh} $tmpI1
  • diameterx=`echo "$diameters" | cut -d, -f1`
  • diametery=`echo "$diameters" | cut -d, -f2`
  • seed1=`echo "$seeds" | cut -d, -f1`
  • seed2=`echo "$seeds" | cut -d, -f2`
  • (
    for ((i=0; i seed1=$((i+seed1))
    rr=`convert xc: -seed $seed1 -format "%[fx:random()]" info:`
    factor=`convert xc: -format "%[fx:$rr>$coverage?$rr:0]" info:`
    hue=`convert xc: -seed $seed2 -format "%[fx:round(100+$factor*random()*200)]" info:`
    echo >&2 "factor=$factor; hue=$hue;"
    convert $tmpI1[$i] -modulate 100,100,$hue miff:-
    ) | convert - -background none -layers merge +repage "$outfile"