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.

LIMITEDTRIM


Trims an image on all sides by a maximum amount specified by the user.

Download Script

last modified: December 15, 2018



USAGE: limitedtrim [-t trimlimit] [-f fuzzval] infile outfile
USAGE: limitedtrim [-h or -help]

-t ... trimlimit ... imit on amount of trim in pixels; integer>=0;
.................... default will limit the trim to smallest trim amount
.................... from all the sides
-f ... fuzzval ..... fuzz value (color tolerance) percent for trimming;
.................... float>=0; default=0

PURPOSE: To trim an image on all sides to a maximum amount specified by the user.

DESCRIPTION: LIMITEDTRIM trims an image on all sides to a maximum amount specified by the user.

ARGUMENTS:

-t trimlimit ... TRIMLIMIT is maximum amount of trim in pixels. Values are integesr>=0. The default will limit the trim to smallest trim amount from all the sides.

-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
(source image)

Original
(size 440x295)

Arguments:
-t 25 -f 5
(size 390x245)

Arguments:
-f 5
(size 412x267)



What the script does is as follows:

  • gets the trim width, height and offsets
  • computes crop width, height and offsets limited by the user supplied argument
  • 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:`
  • 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`
  • if [ "$trimlimit" = "" ]; then
    dx2=$(($ww-$wt-$xoff))
    dy2=$(($hh-$ht-$yoff))
    min=`convert xc: -format "%[fx:min(min(min($xoff,$yoff),$dx2),$dy2)]" info:`
    dx1=$min
    dy1=$min
    nw=$((ww-2*$min))
    nh=$((hh-2*$min))
    else [ $xoff > $trimlimit ] && dx1=$trimlimit || dx1=$xoff
    [ $yoff > $trimlimit ] && dy1=$trimlimit || dy1=$yoff
    dx2=$(($ww-$wt-$xoff))
    dy2=$(($hh-$ht-$yoff))
    [ $dx2 > $trimlimit ] && nw=$((ww-$dx1-$trimlimit)) || nw=$((ww-$dx1-$trimlimit))
    [ $dy2 > $trimlimit ] && nh=$((hh-$dy1-$trimlimit)) || nh=$((hh-$dy1-$dy2))
    fi
  • convert $infile -crop ${wd}x${ht}+${xo}+${yo} +repage $outfile