Fred's ImageMagick Scripts



    Licensing:

    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: http://www.imagemagick.org/script/license.php

OVERLAPCROP


Creates a sequence of cropped subsections permitting optional overlap of the subsections.

Download Script

last modified: September 26, 2017



USAGE: overlapcrop [-s size] [-o overlap] [-m mode] [-u] [-i initnum] [-z zeropad] [-d delay] [-l loop] [-c canvas] [-M] [-L] [-F frame] [-S spacing] [-R] infile outfile

USAGE: overlapcrop [-h or -help]

-s ... size ........ crop subsection dimensions in pixels; one or two
.................... integers>0 with "x" separator; default=128x128
-o ... overlap ..... overlap of subsections; pixels or percent
.................... as one or two integers with "x" separator;
.................... if percent sign included, then it will be converted to
.................... pixels in range 0<=integer<subsection size;
.................... default="50%x50%"
-m ... mode ........ mode of output; choices are matrix (numbered),
.................... sequential (numbered), frames or animation;
.................... default=matrix
-u ... (uniform) ... flag to keep only same size crop sections and discard
.................... smaller pieces on the right and bottom; default is to
.................... keep all crop pieces
-i ... initnum ..... initial number for output naming; integer>=0; default=1
-z ... zeropad ..... number of digits with zero padding for sequential mode;
.................... integer>=0; default=0 (no zero padding)
-d ... delay ....... delay for animation mode; integer>=0; default=50
-l ... loop ........ number of loops for animation mode; integer>=0;
.................... default=0 (endless looping)
-c ... (canvas) .... leave the virtual canvas in the meta data for the images;
.................... default is to remove the virtual canvas
-M ... (montage) ... create single montage image; applicable only to matrix
.................... or sequential mode; default is no montage
-L ... (label) ..... flag to add filename labels to each image in the montage;
.................... default is no filename labels
-F ... frame ....... frame size around each image in the montage; integers>=0;
.................... default=5
-S ... spacing ..... spacing between images in the montage; integers>=0;
.................... default=0
-R ... (remove) .... flag to remove/delete all individual images generated
.................... prior to creating the montage. Default is leave
.................... all individual images.

PURPOSE: To create a sequence of cropped subsections permitting optional overlap of the subsections.

DESCRIPTION: OVERLAPCROP creates a sequence of cropped subsections permitting optional overlap of the subsections. The output images may be numbered either sequentially or in matrix mode (column and row). The images may also be output to a single file with multiple frames, to an animation or to a montage.

ARGUMENTS:

-s size ... SIZE is the crop subsection WidthxHeight dimensions in pixels. Either one or two non-negative integers may be used with "x" separator if two. If only one is provided (with no separator), then it will be used for both width and height. The default="128x128".

-o overlap ... OVERLAP is the amount of overlap of subsections. Overlap may be specified as pixels or percent of subsection size. Values are either one or two non-negative integers with "x" separator. If a percent sign is included, then the overlap will be converted to pixels. Values must be specified (or convert) in the range of zero to the subsection size. The default="50%x50%".

-m mode ... MODE of the output. Choices are matrix (numbered) or m, sequential (numbered) or s, frames (mode) or f and animation (mode) or a. The default=matrix.

-u (uniform) ... Flag to keep only same size crop sections and discard smaller pieces on the right and bottom. The default is to keep all pieces.

-i initnum ... INITNUM is the initial (starting) number for output naming. Values are non-negative integers. The default=1.

-z zeropad ... ZEROPAD is the number of digits with zero padding for numbering the output images in sequential mode. Values are non-negative integers. The default=0 (no zero padding).

-d delay ... DELAY for animation mode. Values are non-negative integers. The default=50.

-l loop ... LOOP is the number of times to loop the animation. Values are non-negative integers. The default=0 (endless looping).

-c (canvas) ... Flag to leave the virtual canvas in the meta data for the images. This is useful when making an animation so that the subsections keep their position relative to the original image. The default is to remove the virtual canvas, which then just has each frame of the animation replacing the previous one.

-M (montage) ... Flag to create a single montage image for matrix or sequential modes only. The default is no montage.

-L (label) ... Flag to add filename labels to each image in the montage. The default is no filename labels.

-F frame ... FRAME is the size of the frame-like border in pixels placed around each image in the montage. Values are non-negative integers. The default=5.

-S spacing ... SPACING is the amount of space in pixels to put between each images in the montage. Values are non-negative integers. The default=0.

-R (remove) ... Flag to remove/delete all individual images generated prior to creating the montage. Th default is to leave all individual images, i.e, no deletion.

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


Matrix Mode Montage

Original Image

Arguments:
-s 128 -o 50% -m matrix -M -L -R



Sequential Mode Montage -- Uniform Size Subsections

Original Image

Arguments:
-s 120 -o 50% -m sequential -u -M -L -R



Sequential Mode Montage -- All Subsections

Original Image

Arguments:
-s 120 -o 50% -m sequential -z 2 -M -L -R



Animation Mode -- Canvas Off

Original Image

Arguments:
-s 120 -o 50% -m animation -d 50 -l 0 -c off



Animation Mode -- Canvas On

Original Image

Arguments:
-s 120 -o 50% -m animation -d 50 -l 0 -c on



What the script does is as follows for matrix output:

  • Computes offsets and ending pixels for x and y
  • Applies a loop for y and a loop for x, computing the subsection WxH+X+Y
    and crops the image, labeling appropriately

This is equivalent to the following IM commands:

  • outname=`echo "$outfile" | sed -n 's/^\(.*\)[\.].*$/\1/p'`
  • suffix=`echo "$outfile" | sed -n 's/^.*[\.]\(.*\)$/\1/p'`
  • ww=`identify -ping -format "%w" $tmpA1`
  • hh=`identify -ping -format "%h" $tmpA1`
  • sw=`echo "${size}" | cut -dx -f1`
  • sh=`echo "${size}" | cut -dx -f2`
  • overlap=`echo "$overlap" | sed 's/%//g'`
  • ox=`echo "${overlap}" | cut -dx -f1`
  • oy=`echo "${overlap}" | cut -dx -f2`
  • ox=`convert xc: -format "%[fx:floor($ox*$sw/100)]" info:`
  • oy=`convert xc: -format "%[fx:floor($oy*$sh/100)]" info:`
  • ow=`convert xc: -format "%[fx:$sw-$ox]" info:`
  • oh=`convert xc: -format "%[fx:$sh-$oy]" info:`
  • ew=`convert xc: -format "%[fx:$ww-$sw+$ox-1]" info:`
  • eh=`convert xc: -format "%[fx:$hh-$sh+$oy-1]" info:`
  • k=0
  • for ((i=0;i<=eh;i+=oh)); do
    for ((j=0;j<=ew;j+=ow)); do
    ((k++))
    done
    done
  • num=$k
  • t=1
  • k=$initnum
  • m=$initnum
  • dh=0
  • for ((i=0;i<=eh;i+=oh)); do
    dw=0
    n=$initnum
    for ((j=0;j<=ew;j+=ow)); do
    echo "Subsection: $t out of $num (${sw}x${sh}+${dw}+${dh}) row=$m col=$n"
    convert $tmpA1[${sw}x${sh}+${dw}+${dh}] +repage ${outname}_r${m}_c${n}.$suffix
    dw=$(($dw+$ow))
    ((t++))
    ((k++))
    ((n++))
    done
    dh=$(($dh+$oh))
    ((m++))
    done