Improved Trim/Border Removal Command?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply

Improved Trim/Border Removal Command?

Post by mankyd » 2007-02-02T09:34:07-07:00

I have been writing a program in Magick++ that loads up several images and does processing on them. The problem is that some of the images have borders (white, black and everything in between) and those borders make the processing that I am doing useless.

I investigated the trim command, but it does not seem to be doing anything for a large number of my images - perhaps because of subtle color variations resulting from JPEG compression?

Here is an example image that I found on flickr It has a large white border: ... c01d2d.jpg

I ran it through the Magick Studio ( ... Studio.cgi) trim command several times but the image never changed.

I noticed that the MagickWand C Library appears to have a trim command that allows for a level of color tolerance. Are there any plans to port this command into other places in ImageMagick, or is there a simple way I can simulate this command in my own code that someone knows of?


Post by mankyd » 2007-02-02T10:46:52-07:00

So I've found something that might work - maybe. First, I found the colorFuzz() setting in Magick++ which I had overlooked before. I don't know if trim() uses this value or not, but I am hoping it does. I couldn't find a way to set this value for trim in Magick Studio (I saw it at the bottom of the front page, but couldn't figure out how to keep it set.)

I also noticed that, in Magick Studio, I had to click the 'reset page geometry' check box on when I trimmed it. I couldn't find a similar command in the Magick++ documentation, (I found page() but that seemed functionally different.)

Many thanks to Anthony Thyssen for his page which helped me out: ... crop/#trim

If anyone knows if this will work, or knows of a good replacement for repage, it would be appreciated. Thanks!

User avatar
Site Admin
Posts: 10963
Joined: 2003-05-31T11:32:55-07:00

Post by magick » 2007-02-02T11:24:38-07:00

Repage is effectively a page setting of 0x0+0+0 (e.g. w/h/x/y all zero).


Post by mankyd » 2007-02-02T13:59:33-07:00

Thanks, magick.

Here's the code I've come up with so far. It's far from perfect, but it is functional for the average case:

Code: Select all

//function continuously trims away the edges of an image, attempting to
//remove any borders. stops when the trim() function fails to chip anything away
void remove_border(Image &img) {
        int pw, ph;
        double fuzz = img.colorFuzz();
        do {
                pw = img.columns();
                ph = img.rows();
        } while (img.columns() != pw || img.rows() != ph);, 0));
A better, more brute force approach, might be to find a way to adjust the colorFuzz according to the standard deviation of colors in the image. I found a sepia toned image that this function ate a large portion of (it didn't actually have a border to begin with!)

User avatar
Posts: 8882
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Improved Trim/Border Removal Command?

Post by anthony » 2007-02-05T21:37:30-07:00

Look a bit further down and try to do a blured trim for 'noisy' images. That will let you trim an image but control and ignore any small scale bits in the image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages

Post Reply