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.

RANDOMCLIPART


Randomly distributes clip art over the image.

Download Script

last modified: January 16, 2022



USAGE: randomclipart [-d dims] [-p percent] [-c color] [-a angles] [-o opacities] infile clipartfiles outfile
USAGE: randomclipart [h|-help]

-d ... dims ...... DIMS is the range of square dimensions of clip art; comma
.................. separate pair of values in range of 0<integer<min image
.................. dimension; default="64,16"
-p ... percent ... PERCENTAGE of clip art coverage across the image;
.................. 0<=integer<=100; default=50
-c ... color ..... COLOR of clip art; any valid Imagemagick color is
.................. allowed or random or image; use image to indicate that
.................. the clip art file is a transparent image and not to be
.................. colored; default=random
-a ... angles .... ANGLES is the range of rotation angles of clip art; comma
.................. separate pair of values in the range of
.................. -360<=integer<=360; default="45,-45"
-o ... opacities . OPACITIES is the range of opacities for of clip art,
.................. but only for random colors; comma separate pair of
.................. values in the range of 0<=integer<=100; default="25,100"
clipartfiles should be either all white on black images to be colored or all transparent images, such as an emojis.

PURPOSE: To randomly distribute clip art over the image.

DESCRIPTION: RANDOMCLIPART randomly distributes clip art over the image. The clip art can have randomized sizes, angles and colors and can be more or less frequently distributed over the image. There is no restriction on the dimensions of the clipartfile, since it will be scaled to have a maximum dimension defined by the dims arguments. The randomization process ensures that no copy of the clip art image will overlap.

ARGUMENTS:

-d dims ... DIMS is the range of scaled values for the maximum dimension of the clip art image. Values are expressed as a comma separate pair (larger first) of values in range of 0<integer<min image dimension. The default="64,16". Best to keep dims much smaller than the minimum input image dimension. If both values are the same or only one value is specified without a comma. The dimension will be a fixed amount. Note that the image will be trimmed on the left and bottom if not a whole multiple of the larger dims argument.

-p percent ... PERCENTAGE distribution of the clip art coverage across the image. Values are 0<=integer<=100. The default=50.

-c color ... COLOR of clip art. Any valid Imagemagick opaque color is allowed or random or image; use image to indicate that the clip art file is a transparent image and not to be colored. The default=random.

-a angles ... ANGLES is the range of rotation angles of the clip art. Values are expressed as a comma separate pair of values (larger first) in the range of -360<=integer<=360. The default="45,-45". If both values are the same or onlyone value is specified without a comma, the rotation will be a fixed amount. Values are specified as "max-angle,min-angle"

-o opacities ... OPACITIES is the range of opacities for the clip art, but only for random colors. Values are comma separate pair of integers in the range of 0<=integer<=100. The default="25,100". If both values are the same or only one value is specified without a comma, the opacity will be a fixed amount. Values are specified as "min-opacity,max-opacity".

The clipartfiles should be either all white on black images to be colored or all transparent images, such as an emojis. There is no restriction on the dimensions of the clipartfile, since it will be scaled to have a maximum dimension defined by the dims arguments.

NOTE: If the bash random value utility, jot, is available the script will run about 30% faster than getting random values using Imagemagick.

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


Original:
barbara

Clip Art:
heart

Arguments:
-d "64,16" -a "45,-45" -p 25 -c yellow



Original:
barbara

Clip Art:
star

Arguments:
-d "64,16" -a "45,-45" -p 35 -c cyan



Original:
barbara

Clip Art:
snowflake

Arguments:
-d "64,16" -a "45,-45" -p 55 -c white



Original:
barbara

Clip Art:
heart

Arguments:
-d "64,16" -a "45,-45" -p 50 -c random



Original:
barbara

Clip Art:
emojis

Arguments:
-d "64,16" -a "45,-45" -p 50 -c image



Original:
barbara

Clip Art:
emojis

Arguments:
-d "64,16" -a "45,-45" -p 50 -c image



Original:
barbara

Clip Art:
emojis

Arguments:
-d "64,16" -a "45,-45" -p 50 -c image



Original:
barbara

Clip Art:
emojis

Arguments:
-d "64,16" -a "45,-45" -p 50 -c image



Original:
barbara

Clip Art:
emojis

Arguments:
-d "64,16" -a "45,-45" -p 50 -c image



What the script does is as follows:

  • Computes a list of unique random blocks of the image of size maxdim
  • For each block, scale and rotate the clip art and optionally colorize it
  • Computes the page offsets for the block
  • flatten all modified copies of the clip art onto the input file

This is equivalent to the following IM commands for randomly colored clipart

  • wh=`convert -ping "$infile" -format "%wx%h" info:`
  • ww=`echo "$wh" | cut -dx -f1`
  • hh=`echo "$wh" | cut -dx -f2`
  • maxdim=`echo "$dims" | cut -d, -f1`
  • mindim=`echo "$dims" | cut -d, -f2`
  • maxangle=`echo "$angles" | cut -d, -f1`
  • minangle=`echo "$angles" | cut -d, -f2`
  • maxscale=100
  • minscale=`convert xc: -format "%[fx:round(100*$mindim/$maxdim)]" info:`
  • ww=`convert xc: -format "%[fx:$maxdim*floor($ww/$maxdim)]" info:`
  • hh=`convert xc: -format "%[fx:$maxdim*floor($hh/$maxdim)]" info:`
  • numx=$((ww/maxdim))
  • numy=$((hh/maxdim))
  • totnum=$((numx*numy))
  • num=`convert xc: -format "%[fx:floor($totnum*$percent/100)]" info:`
  • convert -quiet -regard-warnings "$infile" -crop ${ww}x${hh}+0+0 +repage $tmpA1
  • convert -quiet -regard-warnings $clipartfiles -resize ${maxdim}x${maxdim} $tmpA2
  • list=""
    list=`for ((i=0; i<totnum; i++)); do echo "$i" done`
  • randArr=(`echo "$list" | awk 'BEGIN{srand();}{print rand()" "$0}' | sort -k1 -n | cut -d\ -f2`)
  • diffangle=`convert xc: -format "%[fx:($maxangle-$minangle)]" info:`
  • diffscale=`convert xc: -format "%[fx:($maxscale-$minscale)]" info:`
  • proc_list=""
    for ((i=0; i<num; i++)); do
    tilenum=${randArr[$i]}
    tilenumx=`convert xc: -format "%[fx:mod($tilenum,$numx)]" info:`
    tilenumy=`convert xc: -format "%[fx:floor($tilenum/$numy)]" info:`
    left=`convert xc: -format "%[fx:$tilenumx*$maxdim]" info:`
    top=`convert xc: -format "%[fx:$tilenumy*$maxdim]" info:`
    angle=`jot -r 1 $minangle $maxangle`
    scale=`jot -r 1 $minscale $maxscale`
    layer=`jot -r 1 0 $numm3`
    rr=`jot -r 1 0 255`
    gg=`jot -r 1 0 255`
    bb=`jot -r 1 0 255`
    color="rgb($rr,$gg,$bb)"
    factor=`convert xc: -format \
    "%[fx:$scale/((abs(sin($angle*(pi/180)))+abs(cos($angle*(pi/180)))))]" info:`
    proc_list="$proc_list \( $tmpA2[$layer] -background \"$color\" -alpha shape +repage \
    -scale $factor% -background none -rotate $angle -set page +${left}+${top} \)"
    done
  • eval 'convert -respect-parentheses \( $tmpA1 -set page +0+0 \)' $proc_list '-flatten "$outfile"'