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.

XPAND


Applies a non-uniform outward stretch of the image in one direction.

Download Script

last modified: October 10, 2018



USAGE: xpand -d dimension [-m mode ] [-p protect] infile outfile
USAGE: xpand [h|-help]

-d ... dimension .... dimension for the output image in the stretch direction;
..................... may be expressed either as integer>0 for the width or the height
..................... as dictated by the mode or as a colon separated aspect ratio
..................... (A:B), where the first number (A) is always larger than the
..................... second number (B) and is applied as per the mode; dimension
..................... must specify an output larger than the input; Required Argument
-m ... mode ......... mode for the stretch direction of the output; horizontal (h)
..................... or vertical (v); typically used to expand the same direction
..................... as the input aspect; default=horizontal
-p ... protect ...... protect range of the image as specified by a hyphen separate
..................... increasing pair of integer coordinates within the bounds of
..................... the input image; default is no protect region and the expansion
..................... will be relative to the of the image center.

.

PURPOSE: To apply a non-uniform outward stretch of the image in one direction.

DESCRIPTION: XPAND applies a non-uniform outward stretch of the image in one direction. This elastic expansion increases as one progresses further from the center towards the edges of the image or further from a protect region that may be specified optionally. The scale of the protect region will be preserved in the output image. This process is in effect a dynamic aspect ratio enlargement that preserves the scale of some region of the input image. It is similar to that of the GoPro camera's SuperView. One popular use is to dynamically stretch a 4:3 aspect ratio image to 9:6 aspect ratio. Note that the larger the change in aspect ratio, the more the image will be distorted away from the center or protect region. So this script is usually used for small aspect ratio changes.

ARGUMENTS:

-d dimension ... DIMENSION for the output image in the stretch direction. This may be expressed either as integer>0 for the output width or the height as dictated by the mode (see below) or as a colon separated pair of integers as an aspect ratio (A:B), where the first number (A) is always larger than the second number (B) and is applied as per the mode. The dimension must specify an output no smaller than the input. Required Argument.

-m mode ... MODE for the stretch direction of the output. Choices are: horizontal (h) or vertical (v). Typically, the script is used to expand in the same direction as the aspect of the input. The default=horizontal.

-p protect ... PROTECT range of the image where the scale will not be changed. This is specified by a hyphen separate pair of increasing integer coordinates that fall within the bounds of the input image. The coordinates are both in the same direction of the image as specified by the mode. The first coordinate must be smaller than the second coordinate in the protect range. The default is no protect region and the expansion will be relative to the of the image center.

NOTE: This script may be a bit slow due to the use of -fx.

References:
http://www.semifluid.com/2014/03/16/gopro-superview-like-adaptive-aspect-ratio/
https://www.iamgopro.com/gopro-features/
https://www.goproforums.com/14-video-editing-software/8730-non-linear-stretch-plugin.html

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 - Simple Demonstration - Checkerboard

Original Image

Arguments:
-d "4:3"


Example 2 - Simple Demonstration - Grid Lines on Mandril

Original Image

Arguments:
-d "4:3" -m horizontal
Arguments:
-d "4:3" -m vertical


Example 3

Original Image

Arguments:
-d "2:1" or -d 512
Comarison With -resize 200x100!%


Example 4 - Demonstration of Protect Range (Coke Can)

Original Image

Arguments:
-d 800 -p "50-265"
Comarison With -resize 800x446!


What the script does is as follows when no protect region and mode=horizontal:

  • Reads input
  • Gets the input dimensions and center coordinates
  • Gets the output center coordinates
  • Use an fx expression to process the image
  • Write the output

  • WxH=`convert -ping $tmpI -format "%wx%h" info:`
  • wi=`echo "$WxH" | cut -dx -f1`
  • hi=`echo "$WxH" | cut -dx -f2`
  • wic=`convert xc: -format "%[fx:($wi-1)/2]" info:`
  • hic=`convert xc: -format "%[fx:($hi-1)/2]" info:`
  • woc=`convert xc: -format "%[fx:($dimension-1)/2]" info:`
  • convert -size ${dimension}x${hi} xc: "$infile" \
    -fx "v.p{ ( (i-$woc) + sign(i-$woc)*pow((i-$woc),2)*($wic-$woc)/pow($woc,2) + $wic ),j}" \
    "$outfile"