Fred's ImageMagick Scripts



    Licensing:

    My scripts are available free of charge for non-commercial 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 in the script or not, is restricted to the above licensing arrangements. It is also subject to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

AUTOTRIM


Automatically trims the background border around a normally oriented or rotated image.

Download Script

last modified: April 30, 2013



USAGE: autotrim [-f fuzzval] [-c coords] [-a angle] [-l left] [-r right ] [-t top ] [-b bottom ] infile outfile
USAGE: autotrim [-h or -help]

-f .... fuzzval .... fuzz value for determining border color;
.................... expressed as (float) percent 0 to 100;
.................... default=0 (uniform color)
-c .... coords ..... pixel coordinate to extract color; may be
.................... expressed as gravity value (NorthWest, etc)
.................... or as "x,y" value; default is NorthWest=(0,0)
-a .... angle ...... angle of rotation of image; default indicates
.................... no rotation; angle=0 indicates to autocalculate;
.................... 0<=angle<=45 degrees (float)
-l .... left ....... pixel shift of left edge; +/- is right/left
.................... default=0 (no change)
-r .... right ...... pixel shift of right edge; +/- is right/left
.................... default=0 (no change)
-t .... top ........ pixel shift of top edge; +/- is down/up
.................... default=0 (no change)
-b .... bottom ..... pixel shift of bottom edge; +/- is down/up
.................... default=0 (no change)

PURPOSE: To trim the background border around a normally oriented or rotated image.

DESCRIPTION: AUTOTRIM automatically trims a (nearly) uniform color border around an image. If the image is rotated, one can trim to the bounding box around the image area or alternately trim to the maximum central area that contains no border pixels. The excess border does not have to completely surround the image. It may be only on one side. However, one must identify a coordinate within the border area for the algorithm to extract the base border color and also specify a fuzz value when the border color is not uniform. For simple border trimming of a normally oriented image or the bounding box of a rotated image, you may err somewhat towards larger than optimal fuzz values. For images that contain rotated picture data, when you want to trim to the central area, you should choose the smallest fuzz value that is appropriate. For images that contain rotated picture data, an estimate of the rotation angle is needed for the algorithm to work. However, setting the rotation angle to zero will let the algorithm determine the rotation angle. The resulting trim is usually pretty good for angles >= 5 degrees. If the result is off a little, you may use the left/right/top/bottom arguments to adjust the automatically determined trim region.

ARGUMENTS:

-f fuzzval --- FUZZVAL is the fuzz amount specified as a percent 0 to 100 (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.

-c coords --- COORDS is any location within the border area for the algorithm to find the base border color. It may be specified in terms of gravity parameters (NorthWest, North, NorthEast, East, SouthEast, South, SouthWest or West) or as a pixel coordinate "x,y". The default is the upper left corner = NorthWest = "0,0".

-a angle --- ANGLE is the rotation angle of the picture data within the image. The default (no argument) indicates that either the image does not contain rotated data or one simply wants to trim to the bounding box around the rotated data. If the angle is specified as zero, then the algorithm will automatically estimate the rotation angle which is needed when one wants to trim to the maximum central area of the rotated data which contains no border pixels. One may override the automatic determination and specify your own value. Values are positive floats between 0 and 45 degrees. The rotation direction is not important. Note that the algorithm cannot determine other orientations. You will need to do a 90, 180, or 270 degree rotation before or after using this script.

-l left --- LEFT is the number of extra pixels to shift the trim of the left edge of the image. The trim is shifted right/left for +/- integer values. The default=0.

-r right --- RIGHT is the number of extra pixels to shift the trim of the right edge of the image. The trim is shifted right/left for +/- integer values. The default=0.

-t top --- TOP is the number of extra pixels to shift the trim of the top edge of the image. The trim is shifted down/up for +/- integer values. The default=0.

-b bottom --- BOTTOM is the number of extra pixels to shift the trim of the bottom edge of the image. The trim is shifted down/up for +/- integer values. 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


Trim Of Square Image With Uniform Border Color

2 Pixel White Border

2 Pixel Black Border

2 Pixel White Right Edge

2 Pixel Black Right Edge

Arguments:
(none)

Arguments:
(none)

Arguments:
-c East

Arguments:
-c East



Trim Of Square Image With Non-Uniform Border Color

5 Pixel Radial
Gradient Border

Arguments:
-f 30

Arguments:
-f 35

Arguments:
-f 40

Arguments:
-f 60



Trim Of Rotated Image To Bounding Region

Rotated Image
With 10 Pixel Border

Arguments:
(none)



Trim To Center Area Of Rotated Square Image

Rotated 45 deg

Rotated -20 deg

Rotated 10 deg

Rotated -5 deg

Rotated 2 deg

Arguments:
-a 0

Arguments:
-a 0

Arguments:
-a 0

Arguments:
-a 0

Arguments:
-a 0



Trim To Center Area Of Rotated Rectangular Image

Rotated 45 deg

Rotated -20 deg

Rotated 10 deg

Rotated -5 deg

Arguments:
-a 0

Arguments:
-a 0

Arguments:
-a 0

Arguments:
-a 0



Trim To Center Area Of Rotated Rectangular Image With Extra Trim

Rotated 2 deg

Arguments:
-a 0

Arguments:
-a 0 -l 1 -r -1

Arguments:
-a 0 -l 2 -r -2



What the script does is as follows:

  • Gets the color at the user specified coordinates
  • Pads the image with a 1 pixel border of that color
  • Converts the image to binary format using a fuzz fill
  • Averages the binary image to one row and thresholds at value 1
  • Averages the binary image to one column and thresholds at value 1
  • Determines the left/right trim coordinates from the
    transition from black to white pixel in the row image
  • Determines the top/bottom trim coordinates from the
    transition from black to white pixel in the column image
  • Subsections the input image to generate the output image

This is equivalent to the following IM commands for the case of
a simple trim of the border around an image.

  • color=`convert $infile -format "%[pixel:u.p{$coords}]" info:`
  • convert $infile -bordercolor $color -border 1x1 $infile
  • convert $infile -fuzz $fuzzval% -fill none \
    -draw "matte $coords floodfill" -fill white -colorize 100% $tmp0
  • convert \( -size ${width}x${height} xc:black \) $tmp0 -composite $tmp1
  • convert $tmp1 -filter box -resize 1x${height}! -threshold 1 $tmp2
  • convert $tmp1 -filter box -resize ${width}x1! -threshold 1 $tmp3
  • Multi-line subroutine to get trim coordinates
  • convert $infile[${new_width}x${new_height}+${new_x}+${new_y}] $outfile