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

TRIMCORNERS


Crops the image to removed rounded corners.

Download Script

last modified: October 13, 2017



USAGE: trimcorners [-f fuzzval] [-c ccolor] [-r radius] [-b bias] [-g graphic] [-t thick] [-s scolor] infile outfile
USAGE: trimcorners [-h or -help]

-f ... fuzzval ... fuzz amount in percent for floodfilling the corners; float>=0;
.................. default=5
-c ... ccolor .... corner color for adding a border before the floodfill; any
.................. valid IM color may be specified; default will be determined
.................. automatically from the color of top left corner pixel
-r ... radius .... radius of morphologic close to clean up the mask generated by
.................. the floodfill process; integers>=0; default=1
-b ... bias ...... bias (offsets) from each side to shift and make the crop smaller
.................. in area than the automatically determined size; 4 comma separated
.................. non-negative integers in order of left, top, right, bottom; if
.................. only the first value is specified, it will be used for the
.................. remaining ones; default=0
-g ... graphic ... make an optional output image that is a box drawn on the input
.................. showing where the crop will be made; choices are yes or no;
.................. default=no; if graphic=yes, then the extra image will be the
.................. input name with "_box" added
-s ... scolor .... stroke color to use when drawing the graphic box on the input;
.................. any valid IM color may be specified; default=red
-t ... thick ..... thickness (strokewidth) of the box lines to be drawn when
.................. graphic=yes; integer>=0; default=1

PURPOSE: To automatically crop the image to removed rounded corners.

DESCRIPTION: TRIMCORNERS automatically crops the image to removed rounded corners. A fuzz value is specified to floodfill the corner regions to make a binary mask from the image. The mask will be black in the corners and white elsewhere. From this mask the diagonals of the image are extracted and search from the ends to find the first occurrences of white. The coordinates extracted are then used to create the crop dimensions and the image cropped to form the output.

ARGUMENTS:

-f fuzzval ... FUZZVAL is the fuzz amount in percent for floodfilling the corners. Values are floats>=0. The default=5.

-c ccolor ... CCOLOR is the corner color for adding a border before the floodfill. Any valid IM color may be specified. The default will be determined automatically from the color of top left corner pixel.

-r radius ... RADIUS of morphologic close to clean up the mask generated by the floodfill process; integers>=0; default=1.

-b bias ... BIAS (offsets) from each side to shift and make the crop smaller in area than the automatically determined size. Values are 4 comma separated non-negative integers in order of left, top, right, bottom. If only the first value is specified, it will be used for the remaining ones. The default=0.

-g graphic ... GRAPHIC is a flag to make an optional output image that is a box drawn on the input showing where the crop will be made. The choices are yes or no. The default=no. If graphic=yes, then the extra image will be the input name with "_box" added.

-s scolor ... SCOLOR is the stroke color to use when drawing the graphic box on the input Any valid IM color may be specified. The default=red.

-t thick ... THICK is the hickness (strokewidth) of the box lines to be drawn when graphic=yes. Values are integers>=0. The default=1.

NOTE: If there are regions in the image that touch the corners and have nearly the same color as the corners, then this process may make the crop smaller than desired.

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


Example 1 -- Black Rounded Corners

Input
(source)

Arguments:
-g yes



Example 2 -- White Rounded Corners

Input
(source)

Arguments:
-g yes



Example 3 -- Transparent Rounded Corners

Input
(source)

Arguments:
-g yes



What the script does is as follows:

  • Floodfills the outer regions with transparency
  • Extracts the alpha channel
  • Extracts the two diagonals
  • Searches each end of the diagonals until it finds the first white pixel
  • Gets the coordinates of the located pixels and computes the crop box
  • Crops the image

See the script for coding details