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.

SHUFFLE


Randomly shuffles positions of square sized tiles in an image.

Download Script

last modified: December 15, 2018



USAGE: shuffle [-s size] [-p percent] [-c center] [-r radii] [-R rounding] [-n] [-e ecolor] infile [maskfile] outfile
USAGE: shuffle [h|-help]

-s ... size ....... size of square tiles; integer>0; default=32
-p ... percent .... percentage of tiles to randomly process;
................... 0<=integer<=100; default=100 (all tiles)
-c ... center ..... x and y center coordinates of the round rectangle mask
................... with white on the inside and black on the outside; comma
................... separate pair of integers>=0; default=center of image
-r ... radii ...... x and y radii of a round rectangle mask; comma
................... separated pair of integers>=0; default="0,0" (no mask)
-R ... rounding ... round rectangle corner radii expressed as pair of
................... comma separated integers>=0; default="0,0"
-n ................ negate mask (invert black and white); note only the
................... black area will be diced
-e ... ecolor ..... edge color of mask boundary to show on the resulting
................... image; any valid opaque IM color; default=do not
................... show edge of mask

maskfile is an optional binary (b/w) mask image the same size as the infile

PURPOSE: To randomly shuffle positions of square sized tiles in an image.

DESCRIPTION: SHUFFLE randomly shuffle the positions of square sized tiles in an image. An optional round rectangle mask may be defined to delineate where the shuffling will be shown. Or an external mask image may be provided.

ARGUMENTS:

-s size ... SIZE of the square-sized tiles to randomly shuffle. Values are integers>1. The default=32.

-p percent ... PERCENT is the percentage of patches to randomly shuffle. Values are 0<=integers<=100. The default=100 (all tiles).

-c center ... CENTER is the x and y center coordinates of the round rectangle mask with white on the inside and black on the outside; comma separate pair of integers>=0; default=center of image

-r radii ... RADII are the x and y radii of a round rectangle mask expressed as a comma separated pair of integers>=0. The default="0,0" (no mask).

-R rounding ... ROUNDING is the round rectangle corner radii expressed as a pair of comma separated values. Values are integers>=0. The default="0,0".

-n negate ... NEGATE the mask (invert black and white).

-e ecolor ... ECOLOR is the edge color of the mask boundary to show on the resulting image. Any valid opaque IM color is allowed. The default is not to show the edge boundary on the image.

NOTE: The script runs slowly. A 256x256 sized image processed on my INTEL Mac Mini in about 10 sec for dimension 16 and about 2 sec for dimension 32. Thus larger sized patches will process faster.

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:

Arguments:
(default)



Original:

Arguments:
-p 25



Original:

Arguments:
-s 16 -c 128,128 -r 70,70 -R 35,35 -e blue



Original:

Arguments:
-s 16 -c 128,128 -r 70,70 -R 35,35 -e blue -n



Original:

Arguments:
-s 16 -c 128,128 -r 70,70 -R 35,35



Original:

Mask:

Arguments:
-s 16 -e blue



Original:

Arguments:
(defaults)



What the script does is as follows:

  • Crops the image to a multiple of the dimension
  • Does a dimension-sized tiled crop of image
  • Randomly sorts the tiles
  • Montages all the tiles back to form the output image

This is equivalent to the following IM commands

  • wh=`convert -ping "$infile" -format "%wx%h" info:`
  • ww=`echo "$wh" | cut -dx -f1`
  • hh=`echo "$wh" | cut -dx -f2`
  • ww=`convert xc: -format "%[fx:$dim*floor($ww/$dim)]" info:`
  • hh=`convert xc: -format "%[fx:$dim*floor($hh/$dim)]" info:`
  • numw=$((ww/dim))
  • numh=$((hh/dim))
  • num=$((numw*numh))
  • convert -quiet "$infile" -crop ${ww}x${hh}+0+0 +repage -crop ${dim}x${dim} $tmpA1
  • list=""
    list=`for ((i=0; i<num; i++)); do
    echo "$i"
    done`
    randArr=(`echo "$list" | awk 'BEGIN{srand();}{print rand()" "$0}' | sort -k1 -n | cut -d\ -f2`)
  • (
    for ((i=0; i<num; i++)); do
    index=${randArr[$i]}
    # note need to reset the scene number or montage will try to put back into original order
    convert $tmpA1[$index] +repage -scene 0 miff:-
    done
    ) | montage - -tile ${numw}x${numh} -geometry +0+0 "$outfile"