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.

AUTOTRIM


Automatically trims the outside of an image or crops the inside of a rotated image.

Download Script

last modified: December 15, 2018



USAGE: autotrim [-m mode] [-f fuzzval] [-c coords] [-C color] [-l left] [-r right ] [-t top ] [-b bottom ] [-p preview] infile outfile
USAGE: autotrim [-h or -help]

-m .... mode ....... mode of trim; options are outer (o) or
.................... inner (i); default=outer
-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)
-C ... color ....... border color; any valid IM color is allowed,
.................... default is to use coords to get color
-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)
-p .... preview .... preview displays a red box on input image;
.................... choices are: viewonly (vo), view (v),
.................... save (s) or off (o); default=off

PURPOSE: Automatically trims the outside of an image or crops the inside of a 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. If the result is off a little, you may use the left/right/top/bottom arguments to adjust the automatically determined trim region.

ARGUMENTS:

-m mode ... MODE of trim. The options are either outer (o) or inner (i). The default=outer.

-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 color ... COLOR of border or background. Any valid IM color is allowed. The default is to use coords to get 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".

-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.

-p preview ... PREVIEW displays a red box on the input image. The choices are: viewonly (vo), view (v), saveonly (so), save (s) or off (o). The default=off. If viewonly or saveonly is selected, then no cropped output image will be generated. The preview image, if saved, will be named from the infile as inname_preview.suffix

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:
-m inner

Arguments:
-m inner

Arguments:
-m inner

Arguments:
-m inner

Arguments:
-m inner



Trim To Center Area Of Rotated Rectangular Image

Rotated 45 deg

Rotated -20 deg

Rotated 10 deg

Rotated -5 deg

Arguments:
-m inner

Arguments:
-m inner

Arguments:
-m inner

Arguments:
-m inner



What the script does is as follows for an inner crop:

  • 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
  • Applies a graphical solution to test 8 possible configurations
  • Chooses the solution with the largest area
  • Crops the image according to the corresponding crop box

See script for details