Fred's ImageMagick Scripts
|
Automatically trims the background border around a normally oriented or rotated image. |
last modified: May 09, 2009
|
USAGE: autotrim [-f fuzzval] [-c coords] [-a angle] [-l left] [-r right ] [-t top ] [-b bottom ] infile outfile
-f .... fuzzval .... fuzz value for determining border color; 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. |
|
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: |
Arguments: |
Arguments: |
Arguments: |
||
|
|
|
|
||
|
Trim Of Square Image With Non-Uniform Border Color |
|||||
|
5 Pixel Radial |
Arguments: |
Arguments: |
Arguments: |
Arguments: |
|
|
|
|
|
|
|
|
Trim Of Rotated Image To Bounding Region |
|||||
|
Rotated Image |
Arguments: |
||||
|
|
||||
|
Trim To Center Area Of Rotated Square Image |
|||||
|
Rotated 45 deg |
Rotated -20 deg |
Rotated 10 deg |
Rotated -5 deg |
Rotated 2 deg |
|
|
|
|
|
|
|
|
Arguments: |
Arguments: |
Arguments: |
Arguments: |
Arguments: |
|
|
|
|
|
|
|
|
Trim To Center Area Of Rotated Rectangular Image |
|||||
|
Rotated 45 deg |
Rotated -20 deg |
Rotated 10 deg |
Rotated -5 deg |
||
|
|
|
|
||
|
Arguments: |
Arguments: |
Arguments: |
Arguments: |
||
|
|
|
| ||
|
Trim To Center Area Of Rotated Rectangular Image With Extra Trim |
|||||
|
Rotated 2 deg |
Arguments: |
Arguments: |
Arguments: |
||
|
|
|
|
||
|
What the script does is as follows:
This is equivalent to the following IM commands for the case of
|