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.

SPLITCROP


Crops an image into two or four sections according to the given x,y coordinates.

Download Script

last modified: December 15, 2018



USAGE: splitcrop [-x xcoord] [-y ycoord] infile [outfile]
USAGE: splitcrop [-h or -help]

-x ... xcoord ... x coordinate for split; 0<integer<width; default=center
-y ... xcoord ... y coordinate for split; 0<integer<height; default=center
-L .............. list crop dimensions and offsets to the terminal

Note, the output images will be named automatically from the outfile name and suffix. Two or four of the following: _left, _right, _top, _bottom, _topleft, _topright, _bottomleft, _bottomright will be appended before the suffix. If no outfile is provided, then the infile name and suffix will be used for the output.

PURPOSE: To crop an image into two or four sections according to the given x,y coordinates.

DESCRIPTION: SPLITCROP crops an image into two or four sections according to the given x,y coordinates. One or both of the x,y coordinates may be specified. If one coordinate is specified, then the image will be split into two parts. If two coordinate are specified, then the image will be split both ways into four parts. Note that these are coordinates and not sizes. The top, left or topleft section will include the coordinate specified. The size of the split will be the coordinate plus 1. If the image dimension is odd, then the top, left or topleft will contain the extra pixel(s).

ARGUMENTS:

-x xcoord ... XCOORD is the x coordinate for the split. Values are 0<integers<width. The default=center of image.

-y ycoord ... YCOORD is the y coordinate for the split. Values are 0<integers<height. The default=center of image.

-L ... LIST crop dimensions and offsets to the terminal.

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


Example 2 - X Split

Original Image

Arguments:
-x 127



Example 2 - Y Split

Original Image

Arguments:
-y 127



Example 3 - X and Y Split

Original Image

Arguments:
-x 127 -y 127



What the script does is as follows for matrix output:

  • Computes crop sizes and offsets from the x and/or y coordinates
  • Crops the image appropriately into two or four sections

This is equivalent to the following IM commands:

  • if [ "$x" != "" -a "$y" != "" ]; then
    #split into four parts
    tlsize=`convert xc: -format "%[fx:$x+1]x%[fx:$y+1]+0+0" info:`
    trsize=`convert xc: -format "%[fx:$ww-$x-1]x%[fx:$y+1]+%[fx:$x+1]+0" info:`
    blsize=`convert xc: -format "%[fx:$x+1]x%[fx:$hh-$y-1]+0+%[fx:$y+1]" info:`
    brsize=`convert xc: -format "%[fx:$ww-$x-1]x%[fx:$hh-$y-1]+%[fx:$x+1]+%[fx:$y+1]" info:`
    convert $tmpA1 -crop $tlsize +repage ${outname}_topleft.$suffix
    convert $tmpA1 -crop $trsize +repage ${outname}_topright.$suffix
    convert $tmpA1 -crop $blsize +repage ${outname}_bottomleft.$suffix
    convert $tmpA1 -crop $brsize +repage ${outname}_bottomright.$suffix
    elif [ "$x" != "" ]; then
    #split into two parts horizontally lsize=`convert xc: -format "%[fx:$x+1]x${hh}+0+0" info:`
    rsize=`convert xc: -format "%[fx:$ww-$x-1]x${hh}+%[fx:$x+1]+0" info:`
    convert $tmpA1 -crop $lsize +repage ${outname}_left.$suffix
    convert $tmpA1 -crop $rsize +repage ${outname}_right.$suffix
    elif [ "$y" != "" ]; then
    #split into two parts vertically tsize=`convert xc: -format "${ww}x%[fx:$y+1]+0+0" info:`
    bsize=`convert xc: -format "${ww}x%[fx:$hh-$y-1]+0+%[fx:$y+1]" info:`
    convert $tmpA1 -crop $tsize +repage ${outname}_top.$suffix
    convert $tmpA1 -crop $bsize +repage ${outname}_bottom.$suffix
    fi