Page 1 of 1

Enhanced -trim operator

Posted: 2019-03-03T13:08:38-07:00
by magick
As of ImageMagick 7.0.8-31 and 6.9.10-31,

Code: Select all

-trim
has new functionality to aggressively trim away the background from an image. Rows and columns will be removed iteratively from the outside until each remaining ones contain only a specified percentage of background color.

The new functionality is controlled by a define. The define is

Code: Select all

-define trim:percent-background=n%

where n is the percentage of background pixels permitted in the outer rows and columns of the image. That is, rows and columns of the image will be removed from the outside of the image until each row or column contains no more than n% background color pixels.

To remove all outer rows or columns with background color pixels, use

Code: Select all

-define trim:percent-background=0%

Note, the trim option is responsive to the fuzz setting.

Here are some examples.

Image

This command removes all black background

Code: Select all

magick lena_r5.png -fuzz 10% -define trim:percent-background=0% -trim lena_r5_fuzz10_trim0.png
Image

This command removes all rows and columns until only 50% background remains in each.

Code: Select all

magick lena_r5.png -fuzz 10% -define trim:percent-background=50% -trim lena_r5_fuzz10_trim50.png
Image

Here is another example trimming until 0% background.

Image

Code: Select all

magick young_girl2_roundrect.jpg -fuzz 5% -define trim:percent-background=0% -trim young_girl2_roundrect_fuzz5_trim0.jpg
Image


These last two example show what happens with too large a fuzz value

Image

Code: Select all

magick horizon_sunset_border2.jpg -fuzz 5% -define trim:percent-background=0% -trim horizon_sunset_border2_fuzz5_trim0.jpg
Image

Code: Select all

magick horizon_sunset_border2.jpg -fuzz 10% -define trim:percent-background=0% -trim horizon_sunset_border2_fuzz10_trim0.jpg
Image

Re: Enhanced -trim operator

Posted: 2019-10-13T17:09:42-07:00
by fmw42
Here is another interesting example -- see my Imagemagick solution for trimming and appending fisheye stereo pairs side-by-side at https://stackoverflow.com/questions/583 ... tereo-rect

Note a new argument for the aggressive -trim has been added in IM 6.9.10.69 beta and IM 7.0.8.69 beta: -define trim:background-color=<color> to ensure it only trims a particular color.