Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

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

INNERCROP


Crops an image to the largest rectangle that will just trim any irregularly shaped outer boundary color.

Download Script

last modified: June 22, 2015



USAGE: innercrop [-o ocolor] [-p prep] [-f fuzzval] [-t thresholds] [-b biases] [-u ucolor] [-m mode] [-g gcolor] [-s swidth] [-i is360] [-r rmse] infile outfile
USAGE: innercrop [-help]

-o .... ocolor .......... color of outer region to be cropped out;
......................... Any valid IM color; default=black
-p .... prep ............ preprocessing trim type; choices are 1 or 2;
......................... 1=fuzzy trim; 2=fuzzy floodfill trim; default=1
-f .... fuzzval ......... fuzz value in percent to use for preprocessing;
......................... 0<=float<=100; default=0
-t .... thresholds ...... four optional threshold values, one for each
......................... processing pass; comma separate list; values
......................... are floats>=0; default=",,,," means all four
......................... are computed automatically
-b .... biases .......... four optional biases applied as multipliers of
......................... automatic thresholds, only; values are floats>=0;
......................... default="1,1,1,1"
-u .... ucolor .......... unused color in image to use when converting
......................... image to a binary mask; Any valid IM color;
......................... default="none"
-m .... mode ............ mode for output image; choices are crop and box;
......................... crop means to crop the input image; box means
......................... to draw a rectangular box showing where the image
......................... would be cropped; default=crop
-g .... gcolor .......... color to use when mode=box; Any valid IM color;
......................... default=red
-s .... swidth .......... strokewidth to use when mode=box; integer>0;
......................... default=1
-i .... is360 ........... test if cropped image is a 360 panorama;
......................... yes means do test; no means do not do test;
......................... default=no
-r .... rmse ............ rmse threshold value for determining if is 360
......................... panorama; float>0; default=.05

PURPOSE: To crop an image to a rectangle that will just trim any irregularly shaped outer boundary color.

DESCRIPTION: INNERCROP crops an image to a rectangle that will just trim any irregularly shaped outer boundary color. The image is first trimmed and then converted to a binary mask. The mask is processed to one column and one row and thresholded to find the first and last occurences of white, which determine the top and bottom and left an right sides for cropping. This is repeated a second time to get the final crop coordinates.

ARGUMENTS:

-o ocolor ... OCOLOR is the color of the outer region that is to be cropped out of the image. Any valid IM color is allowed. See http://imagemagick.org/script/color.php

-p prep ... PREP is the preprocessing trim type to be used. Choices are either 1 or 2. A value of 1 means to simply do a fuzzy trim. A value of 2 means to do a fuzzy floodfill trim. The latter is useful when the image itself contains the ocolor interior to the outer region. The default=1

-f fuzzval ... FUZZVAL is the fuzz factor in percent used in the preprocessing step. The choice of value is important if the outer color is not uniform as may be the case with jpeg compression. Typical values in those cases are a few percent. Over cropping can occur if the fuzzval is too high. Values are 0<=float<=100. The default=0

-t thresholds ... THRESHOLDS are four comma separate combinations of non-negative floating point values or null values (no spaces). The default=",,," which indicates that each of the four thresholds is to be computed automatically. Non-null values may be useful if the automatic thresholds do not provide a good result and one desires to try to improve the result.

-b biases ... BIASES are four comma separate non-negative floating point values that are used as multipliers of the automatically computed thresholds. The corresponding bias is not used when a non-null value is proved for a given threshold. The default="1,1,1,1". The biases may be useful if the automatic thresholds do not provide a good result and one desires to try to improve the result.

-u ucolor ... UCOLOR is any unused (safe) color in the image that will be used to convert the image into a mask. Any valid IM color is allowd. The default=none (i.e. transparent) and assumes the image is fully opaque. See http://imagemagick.org/script/color.php

-m mode ... MODE is the output mode. The choices are: crop and box. When mode=crop, the ouput image will be the cropped version of the input image. When mode=box, a graphic box outline will be drawn on the input image showing where the cropping would take place. The default=crop.

-g gcolor ... GCOLOR is the color of the graphic box outline to be used when mode=box. Any valid IM color is allowed. The default=red. See http://imagemagick.org/script/color.php

-s swidth ... SWIDTH is the strokewidth to use when drawing the graphic box outline. Values are integers>0. The default=1.

-i is360 ... IS360 is a flag to perform a test to determine if the image is a 360 degree panorama. The choices are yes or no. The default=no, which means do not test for 360 degree panorama.

-r rmse ... RMSE is the root mean squared error threshold used to compare the first and last columns to determine if they are similar enough. Values are floats>0. The default is 0.05. If the compare test has an rmse value less than this threshold, then the image is determined to be a 360 degree panorama.

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 Image

Arguments:
-f 5 -m box

Arguments:
-f 5 -m crop



Original Image

Arguments:
-f 4 -m box

Arguments:
-f 4 -m crop



Original Image

Arguments:
-f 4 -b "2,,," -m box
or
-f 4 -t "43,,," -m box

Arguments:
-f 4 -b "2,,," -m crop
or
-f 4 -t "43,,," -m crop



Original Image

Arguments:
-f 4 -m box

Arguments:
-f 4 -m crop



What the script does is as follows:

  • Trims the input image border
  • Creates a binary mask from the image
  • Scales the mask down to one column and thresholds it
  • Finds the first and last occurence of a white pixel to get y1 and y2
  • Crops the image in y
  • Scales the mask down to one row and thresholds it
  • Finds the first and last occurence of a white pixel to get x1 and x2
  • Crops the image in y
  • Repeats in the process above to refine y1,y2 and x1,x2
  • Crops the original image using the refined coordinates
  • Outputs the resulting cropped image