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.

ASPECTPAD


Pads an image with a color to a specified aspect ratio and orientation.

Download Script

last modified: December 15, 2018



USAGE: aspectpad [-a aspect] [-m mode] [-p pcolor] [-t toler] infile outfile
USAGE: aspectpad [-help]

-a .... aspect ...... aspect ratio value desired; float>=1; default=2
-m .... mode ........ mode for aspect; al, ap, l or p; default=al
..................... l=force landscape pad; p=force portrait pad;
..................... al=automatic pad (landscape for square images);
..................... ap=automatic pad (portrait for square images)
-p .... pcolor ...... pad color; any valid IM color; default=black
-t .... toler ....... aspect tolerance; float>=0; if absolute difference
..................... between desired aspect and image aspect is less
..................... than or equal to tolerance, then no padding;
..................... default=0

PURPOSE: To pad an image with a color to a specified aspect ratio and orientation.

DESCRIPTION: ASPECTPAD pads an image with a color to a specified aspect ratio and orientation. The user can choose to force the pad to either landscape or portrait orientation or preserve the orientation in automatic mode. All padding will result in the image being centered.

ARGUMENTS:

-a aspect ... ASPECT is the desired aspect ratio. Values are floats>=1. The default=2

-m mode ... MODE is the padding mode. Choices are: l, p, al or ap. When mode=l, the padding will force the result to be landscape at the desired aspect value. When mode=p, the padding will force the result to be portrait. When mode=al, the padding will preserve the aspect of the original image, but will pad a square image into landscape format. When mode=ap, the padding will preserve the aspect of the original image, but will pad a square image into portrait format. The default=al.

-p pcolor ... PCOLOR is the desired padding color. Any valid IM color specification may be used. The default=black

-t toler ... TOLER is the aspect tolerance. If the absolute difference between desired aspect and image aspect is less than or equal to toler, then no padding will be applied. Values are floats>=0. The default=0

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 1

Original

Arguments:
-a 2 -m al -p "#425C2C"

Arguments:
-a 1.3333 -m al -p "#425C2C"

size 120x80 (3:2)

size 160x80 (2:1)

size 120x90 (4:3)



Example 2

Original

Arguments:
-a 1.5 -m al -p black

Arguments:
-a 1 -m al -p black

Arguments:
-a 1.5 -m l -p black

size 80x101 (4:5)

size 80x120 (2:3)

size 101x101 (1:1)

size 152x101 (3:2)



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 -extent to pad the image with the desired color

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

  • ww=`convert $infile -ping -format "%w" info:`
  • hh=`convert $infile -ping -format "%h" info:`
  • wratio=`convert xc: -format "%[fx:$ww/$hh]" info:`
  • hratio=`convert xc: -format "%[fx:$hh/$ww]" info:`
  • wtest=`convert xc: -format "%[fx:$aspect>=$wratio?1:0]" info:`
  • htest=`convert xc: -format "%[fx:$aspect>=$hratio?1:0]" info:`
  • if [ $ww -eq $hh ]; then
    ww=`convert xc: -format "%[fx:$hh*$aspect]" info:`
    convert $infile -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
    elif [ $ww -gt $hh -a $wtest -eq 1 ]; then
    ww=`convert xc: -format "%[fx:$hh*$aspect]" info:`
    convert $infile -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
    elif [ $ww -gt $hh -a $wtest -eq 0 ]; then
    hh=`convert xc: -format "%[fx:$hh*$wratio/$aspect]" info:`
    convert $infile -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
    elif [ $hh -gt $ww -a $htest -eq 1 ]; then
    hh=`convert xc: -format "%[fx:$ww*$aspect]" info:`
    convert $infile -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
    elif [ $hh -gt $ww -a $htest -eq 0 ]; then
    ww=`convert xc: -format "%[fx:$ww*$hratio/$aspect]" info:`
    convert $infile -gravity center -background "$pcolor" -extent ${ww}x${hh} $outfile
    fi