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

SQUAREUP


Resizes an image and squares it up either by padding or cropping

Download Script

last modified: September 26, 2017



USAGE: squareup [-s size] [-m mode] [-c color] [-g gravity] [-f filter] infile outfile
USAGE: squareup [-h or -help]

-s .... size ......... desired size; value in pixels or value% as scale factor;
...................... default is same size as input
-m .... mode ......... pad or crop; default is pad
-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 and square it up either by padding or cropping.

DESCRIPTION: SQUAREUP resizes an image and squares it up either by padding it with a constant background color or transparent background or crops it. If padded, the larger dimension will be resized and the smaller dimension will be padded. If cropped, the smaller dimension will be resized and the larger dimension will be cropped. The result will be a square image, i.e. the width and height will be identical.

ARGUMENTS:

-s size ... SIZE specifies the desired output size. The value may be specified as an integer either in pixels or as floating point scale factor in percent (including the % symbol). The default, if left off, is to keep the image the same size.

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

-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


Taller Than Wide Image -- Variations Of Parameters

Original Image

Arguments:
-m pad (default)

Arguments:
-m pad -s 128

Arguments:
-m pad -s 128 -c maroon

Arguments:
-m pad -s 50%

Arguments:
-m crop

Arguments:
-m crop -s 128

Arguments:
-m crop -s 128 -g South

Arguments:
-m crop -s 50%



Wider Than Tall Image -- Variations Of Parameters

Original Image

Arguments:
-m pad (default)

Arguments:
-m pad -s 128

Arguments:
-m pad -s 50%

Arguments:
-m crop

Arguments:
-m crop -s 128

Arguments:
-m crop -s 50%



What the script does is as follows:

  • Deterines the width and height of the image and the
    larger and smaller of the these dimensions
  • If pad, then use the larger dimension to resize; otherwise
    if crop, then use the smaller dimension to resize
  • Pads the image with background color or crops the image
    according to the gravity setting to keep the result square

This is equivalent to the following IM commands for
the case of resizing a taller than wide image to 128x128 pixels

  • if [ "$mode" = "pad" ]
    then
    convert \( -size 128x128 xc:black \) \
    \( -filter lanczos -resize x128 $infile \) \
    -gravity center -composite +repage $outfile
    elif [ "$mode" = "crop" ]
    then
    convert $infile -filter lanczos -resize 128x \
    -gravity center -crop 128x128+0+0 +repage $outfile
    fi