Fred's ImageMagick Scripts


    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:


Crops an image to a specified aspect ratio.

Download Script

last modified: October 12, 2014

USAGE: aspectcrop [-a aspect] [-g gravity] infile outfile
USAGE: aspectcrop [-help]

-a ... aspect .... aspect ratio value desired; width to height ratio; values
.................. can be either a float or as two floats separated by a
.................. colon; default is input aspect ratio (no change)
-g ... gravity ... gravity alignment for cropping; any IM -gravity value;
.................. default=center

PURPOSE: To crop an image to a specified aspect ratio.

DESCRIPTION: ASPECTPAD crops an image to a specified aspect ratio.


-a aspect ... ASPECT is the desired width to height aspect ratio. Values are either floats>0, such as 2 (landscape) or 0.5 (portrait), or two floats separate by a colon (and no spaces), such a 2:1 (landscale) or 1:2 (portrait). The default is input aspect ratio (no change).

-g gravity ... GRAVITY alignment for cropping; Any IM -gravity setting is allowed. Options are: center (c), north (n), south (s), east (e), west (w), northwest (nw), northeast (ne), southwest, (sw) or southeast (se). The default=center.

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


-a 1:1

-a 1:2

size 265x333

size 265x265

size 167x333

Example 2

size 400x299

-a 1:1

size 299x299

-a 2:1

size 400x200

-a 2:1 -g south

size 400x200

What the script does is as follows:

  • gets the aspect ratio of the image and compares it to
    the desired aspect
  • computes a new releveant width or height from these aspect ratios
  • uses -crop to crop the image

This is equivalent to the following IM commands for the case of automatic mode

  • ww=`convert $tmpA1 -ping -format "%w" info:`
  • hh=`convert $tmpA1 -ping -format "%h" info:`
  • ratio=`convert xc: -format "%[fx:$ww/$hh]" info:`
  • aspect1=`echo $aspect | cut -d\: -f1`
  • aspect2=`echo $aspect | cut -d\: -f2`
  • test=`convert xc: -format "%[fx:($aspect2 == $aspect1)?1:0]" info:`
  • if [ "$aspect2" = "" ]; then
    elif [ $test -eq 1 ]; then
    aspect=`convert xc: -format "%[fx:$aspect1/$aspect2]" info:`
  • test=`convert xc: -format "%[fx:$aspect>=$ratio?1:0]" info:`
  • [ $test -eq 1 ] && format="larger" || format="smaller"
  • if [ "$format" = "larger" ]; then
    height=`convert xc: -format "%[fx:$hh*$ratio/$aspect]" info:`
    elif [ "$format" = "smaller" ]; then
    width=`convert xc: -format "%[fx:$ww*$aspect/$ratio]" info:`
  • convert $tmpA1 -gravity $gravity -crop ${width}x${height}+0+0 +repage $outfile