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.

TRIMMER


Trims the background from any number of specified sides of an image.

Download Script

last modified: September 05, 2019



USAGE: trimmer [-s sides] [-f fuzzval] [-b bcolor] [-g gcolor] infile outfile
USAGE: trimmer [-h or -help]

-s ..... sides ...... comma delimited list of sides to trim; list can include
..................... any combination of: north, east, south, west or just "all";
..................... default=north
-f ..... fuzzamt .... fuzz amount in percent for trimming border color; float;
..................... fuzzamt>=0; default=0
-b ..... bcolor ..... background color for area to trim; any valid IM color
..................... may be specified; default will be determined automatically
..................... from the average color of all specified sides
-g ..... gcolor ..... guard color used to preserve sides not be trimmed;
..................... any valid IM color that is further from the background color
..................... than the fuzzval; default will be determined automatically
..................... as either the complement of the background color, black or
..................... white whichever is furthest from the background color.

PURPOSE: To trim the background from any number of specified sides of an image.

DESCRIPTION: TRIMMER trims the background from any number of specified sides of an image. The user may specify from one to four sides as a comma delimited list that is enclosed in quotes. A fuzz value may be specified if the background is not a constant color.

ARGUMENTS:

-s sides --- SIDES is a comma delimited list of the sides to be trimmed enclosed in quotes. Any number of sides from one to four may be specified. The list can include any combination of: north, east, south, west or just "all". The default=north.

-f fuzzamt --- FUZZAMT is the fuzz amount specified as a float percent greater than zero (without the % sign). The default is zero which indicates that border is a uniform color. Larger values are needed when the border is not a uniform color.

-b bcolor --- BCOLOR is the background color for the area to trim. Any valid IM color may be specified. The default will be determined automatically from the average color of all specified sides. There is no guarantee that this will be accurate. So if it does not work, then you will need to supply the border color.

-g gcolor --- GCOLOR is the guard color used to preserve the side that are not to be trimmed. Any valid IM color may be specified that is further from the background color than the fuzzval. The default will be determined automatically as either the complement of the background color, black or white whichever is furthest from the background color. There is no guarantee that this will be accurate. So if it does not work, then you will need to supply the guard color.

NOTE: This script will not trim more than half way in any dimension. Also for images with transparency, you will have to provide a guard color.

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


Original

Arguments:
-s north -f 20

Arguments:
-s east -f 20

Arguments:
-s south -f 20

Arguments:
-s west -f 20

 

 

   

Arguments:
-s "north,south" -f 20

Arguments:
-s "east,west" -f 20

Arguments:
-s "north,east" -f 20

Arguments:
-s "north,west" -f 20

Arguments:
-s "south,east" -f 20

Arguments:
-s "south,west" -f 20

Arguments:
-s "north,east,south" -f 20

Arguments:
-s "north,west,south" -f 20

Arguments:
-s "west,north,east" -f 20

Arguments:
-s "west,south,east" -f 20

 

 

   

Arguments:
-s "north,east,south,west" -f 20

Arguments:
-s "all" -f 20

 

 

 

 

       


What the script does is as follows:

  • Converts the list of sides to remove to an array of sides to preserve
  • Splices one row or column of background color to the sides to preserve
  • Composites a 1x1 pixel of guard color to the center of those spliced rows and columns
  • Trims the image using -trim and the fuzz amount
  • Chops off the protected (spliced) rows and columns

This is equivalent to the following IM commands for the case of removing the south side.

  • convert $infile \
  • -background white -gravity north -splice 0x1+0+0 \
  • -background white -gravity east -splice 1x0+0+0 \
  • -background white -gravity west -splice 1x0+0+0 \
  • -size 1x1 xc:black -gravity north -compose over -composite \
  • -size 1x1 xc:black -gravity east -compose over -composite \
  • -size 1x1 xc:black -gravity west -compose over -composite \
  • -trim +repage \
  • -gravity north -chop 0x1+0+0 \
  • -gravity east -chop 1x0+0+0 \
  • -gravity west -chop 1x0+0+0 \
  • $outfile