Enhanced -trim operator
Posted: 2019-03-03T13:08:38-07:00
As of ImageMagick 7.0.8-31 and 6.9.10-31, 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
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
Note, the trim option is responsive to the fuzz setting.
Here are some examples.
This command removes all black background
This command removes all rows and columns until only 50% background remains in each.
Here is another example trimming until 0% background.
These last two example show what happens with too large a fuzz value
Code: Select all
-trim
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.
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
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
Here is another example trimming until 0% background.
Code: Select all
magick young_girl2_roundrect.jpg -fuzz 5% -define trim:percent-background=0% -trim young_girl2_roundrect_fuzz5_trim0.jpg
These last two example show what happens with too large a fuzz value
Code: Select all
magick horizon_sunset_border2.jpg -fuzz 5% -define trim:percent-background=0% -trim horizon_sunset_border2_fuzz5_trim0.jpg
Code: Select all
magick horizon_sunset_border2.jpg -fuzz 10% -define trim:percent-background=0% -trim horizon_sunset_border2_fuzz10_trim0.jpg