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. |
Automatically trims the sides of an image until no outer row or column contains the background color. |
last modified: December 29, 2018
USAGE: trim2rect [-f fuzzval] [-c coords] [-b bgcolor] [-p progress] [-g graphic]
[-d dcolor] infile outfile
-f ... fuzzval .... fuzz value for determining border color; expressed as (float) PURPOSE: Automatically trims the sides of an image until no outer row or column contains the background color. DESCRIPTION: TRIM2RECT automatically trims the sides of an image one row or column at a time until no side row or column contains the background color. The background color is first floodfilled to be transparent and a -trim is performed to remove excess background color that is completely along any side. Then the alpha channel is extracted. Each side row or column of the alpha channel is then tested for its mean value. If the mean value is less than one, a counter is incremented for that side. Once a side row or column of the alpha channel has a mean of 1, that side row or column is no longer tested. Processing continues until all side rows and columns have a mean value of 1. A crop box is then computed from the counters indicating how many rows and columns should be removed from each side. Finally the image is cropped. ARGUMENTS: -f fuzzval ... FUZZVAL is the fuzz amount specified as a percent 0 to 100 (without the % sign). The default=5. A value of zero 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". -bg color ... BGCOLOR is the background color surrounding the region of interest. Any valid IM color is allowed. The default is to use coords to get color. The bgcolor takes precedence over coordinates. -p progress... show PROGRESS in cycles. The choices are: yes (y) or no (n). Each cycle corresponds to a test of all four sides in the order of top, left, bottom, right. -g graphic ... GRAPHIC specifies to draw the crop box on input image after the initial -trim to remove excess border. Choices are: yes (y) or no (n). The default=no. The graphic image will be the input image name with _cropbox.jpg appended. -d dcolor ... DCOLOR is the crop box draw color. Any valid opaque IM color is allowed. The default=red. Note, this script may be quite slow as it processes one row or column at a time. 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. |
Example 1 |
Input |
Arguments: |
83 iterations; 41 sec |
Example 2 |
Input |
Arguments: |
33 iterations; 17 sec |
Example 3 |
Input |
Arguments: |
183 iterations; 1 min 30 sec |
Example 4 |
Input |
Arguments: |
167 iterations; 1 min 23 sec |
What the script does is as follows:
See the script for coding details |