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.

CENTERTRIM


Trims an image so as to preserve the image center.

Download Script

last modified: December 15, 2018



USAGE: centertrim [-f fuzzval] infile outfile
USAGE: centertrim [-h or -help]

-f ... fuzzval ... fuzz value (color tolerance) percent for trimming;
.................. float>=0; default=0

PURPOSE: To trim an image so as to preserve the image center.

DESCRIPTION: CENTERTRIM trims an image so as to preserve the image center based upon the most distant trimmed corner from the center.

ARGUMENTS:

-f fuzzval ... FUZZVAL is fuzz value (color tolerance) percent associated with trimming. Values are floats greater than or equal to zero. 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:
-f 1



Example 2

Original

Arguments:
-f 1



Example 3

Original

Arguments:
-f 1



What the script does is as follows:

  • gets the trim width, height and offsets
  • computes crop width, height and offsets based up the trim corner
    furthest from the center
  • uses crops the image

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

  • ww=`convert -ping "$infile" -format "%w" info:`
  • hh=`convert -ping "$infile" -format "%h" info:`
  • cx=`convert xc: -format "%[fx:($ww/2)]" info:`
  • cy=`convert xc: -format "%[fx:($hh/2)]" info:`
  • triminfo=`convert "$infile" -fuzz $fuzzval% -format "%@" info:`
  • #echo "triminfo=$triminfo"
  • wt=`echo "$triminfo" | cut -d+ -f1 | cut -dx -f1`
  • ht=`echo "$triminfo" | cut -d+ -f1 | cut -dx -f2`
  • xoff=`echo "$triminfo" | cut -d+ -f2`
  • yoff=`echo "$triminfo" | cut -d+ -f3`
  • d1x=`convert xc: -format "%[fx:(abs($xoff-$cx))]" info:`
  • d1y=`convert xc: -format "%[fx:(abs($yoff-$cy))]" info:`
  • d2x=`convert xc: -format "%[fx:(abs($xoff+$wt-$cx))]" info:`
  • d2y=`convert xc: -format "%[fx:(abs($yoff-$cy))]" info:`
  • d3x=`convert xc: -format "%[fx:(abs($xoff+$wt-$cx))]" info:`
  • d3y=`convert xc: -format "%[fx:(abs($yoff+$ht-$cy))]" info:`
  • d4x=`convert xc: -format "%[fx:(abs($xoff-$cx))]" info:`
  • d4y=`convert xc: -format "%[fx:(abs($yoff+$ht-$cy))]" info:`
  • d1=`convert xc: -format "%[fx:(hypot($d1x,$d1y))]" info:`
  • d2=`convert xc: -format "%[fx:(hypot($d2x,$d2y))]" info:`
  • d3=`convert xc: -format "%[fx:(hypot($d3x,$d3y))]" info:`
  • d4=`convert xc: -format "%[fx:(hypot($d4x,$d4y))]" info:`
  • dmax=`convert xc: -format "%[fx:max(max(max($d1,$d2),$d3),$d4)]" info:`
  • if [ "$d1" = "$dmax" ]; then
    wd=`convert xc: -format "%[fx:2*$d1x]" info:`
    ht=`convert xc: -format "%[fx:2*$d1y]" info:`
    xo=`convert xc: -format "%[fx:($xoff)]" info:`
    yo=`convert xc: -format "%[fx:($yoff)]" info:`
    elif [ "$d2" = "$dmax" ]; then
    wd=`convert xc: -format "%[fx:2*$d2x]" info:`
    ht=`convert xc: -format "%[fx:2*$d2y]" info:`
    xo=`convert xc: -format "%[fx:($cx-$d2x)]" info:`
    yo=`convert xc: -format "%[fx:($yoff)]" info:`
    elif [ "$d3" = "$dmax" ]; then
    wd=`convert xc: -format "%[fx:2*$d3x]" info:`
    ht=`convert xc: -format "%[fx:2*$d3y]" info:`
    xo=`convert xc: -format "%[fx:($cx-$d3x)]" info:`
    yo=`convert xc: -format "%[fx:($cy-$d3y)]" info:`
    elif [ "$d4" = "$dmax" ]; then
    wd=`convert xc: -format "%[fx:2*$d4x]" info:`
    ht=`convert xc: -format "%[fx:2*$d4y]" info:`
    xo=`convert xc: -format "%[fx:($xoff)]" info:`
    yo=`convert xc: -format "%[fx:($cy-$d4y)]" info:`
    fi
  • convert $infile -crop ${wd}x${ht}+${xo}+${yo} +repage $outfile