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.

ASPECT


Resizes an image to a specific size allowing either cropping or padding to deal with the aspect ratio change

Download Script

last modified: December 15, 2018



USAGE: aspect widthxheight [-m mode] [-c color] [-g gravity] [-f filter] infile outfile
USAGE: aspect [-h or -help]

widthxheight ......... desired size (width x height); value in pixels
-m .... mode ......... crop or pad; default is crop
-c .... color ........ background color if pad; any IM color specification
...................... or none or trans for transparent; default is black
-g .... gravity ...... gravity location for cropped area; North, East, South
...................... West or Center; default is Center
-f .... filter ....... resize filter; any valid IM filter; default is Lanczos

PURPOSE: To resize an image to a specific size allowing either cropping or padding to deal with the aspect ratio change.

DESCRIPTION: ASPECT resizes an image to a specific size allowing cropping or padding with a constant background color or transparent background. In either case, the aspect ratio of the image will be compared to the aspect ratio of the desired size and cropped or padded to fill one or the other dimension accordingly.

ARGUMENTS:

widthxheight ... WIDTHxHEIGHT specifies the desired output size. WIDTH and HEIGHT values must be specified as integer pixels.

-m mode ... MODE specifies to either pad or crop the image to square it off. The default is crop.

-c color ... COLOR specifies the background color to use when mode=pad. Any valid IM color specification may be used. To make the background transparent, set the color either to none or trans. The default is black.

-g gravity ... GRAVITY is the region of the image to use when mode=crop. Values may be North, East, South, West or Center. The default is Center.

-s sat ... SAT specifies a gain in saturation. Values for sat must be non-negative integers. A value of 100 indicates no saturation change. Larger/smaller values indicate more/less saturation.

-f filter ... FILTER is any valid IM resize filter. The default is Lanczos.

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


Wider Than Tall Image

Original Image

Arguments:
100x80 -m crop

Arguments:
100x80 -m pad

Arguments:
80x100 -m crop

Arguments:
80x100 -m pad



Square Image

Original Image

Arguments:
100x80 -m crop

Arguments:
100x80 -m pad

Arguments:
80x100 -m crop

Arguments:
80x100 -m pad



Taller Than Wide Image

Original Image

Arguments:
100x80 -m crop

Arguments:
100x80 -m pad

Arguments:
80x100 -m crop

Arguments:
80x100 -m pad



What the script does is as follows:

  • Determines the width and height of the image and its
    aspect ratio and the aspect ratio of the output size
  • Resize the image to widthxwidth, heightxheight,
    widthxwidth^ or heightxheight^ depending upon aspect
    ratios and whether crop or pad.
  • If pad, then insert into constant background color image
    of desired size

This is equivalent to the following IM commands for the
case of resizing a wider than tall image to 100x80 pixels

  • if [ "$mode" = "crop" ]
    then
    convert $infile -filter lanczos -resize 80x80^ \
    -gravity Center -crop 100x80+0+0 \
    +repage $outfile
    elif [ "$mode" = "pad" ]
    then
    convert \( -size 100x80 xc:black \) \
    \( -filter lanczos -resize 100x100 $infile \) \
    -gravity Center -composite +repage $outfile
    fi