Enhanced -trim operator

Announcements pertaining to ImageMagick, or ImageMagick related software. This list is moderated. No discussions here, instead post to the users group instead.
Post Reply
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Enhanced -trim operator

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Enhanced -trim operator

Post 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.
Post Reply