Page 1 of 1

Help! Poor results from Trim

Posted: 2013-01-06T17:04:58-07:00
by arnbrhm
I'm new to ImageMagick and have spent all day trying how to perform what would seem to be an easy trim of a white border. I have a lot of postcard size documents I need to scan and remove the white border left by the scanner. After many failed attempts, I thought I would create a sample of graphic in paint and try to get trim to work on that first.

Note: I'm using the Windows version. Once I find the commands that work, I can automate everything using AutoIt.

Here is my box.jpg graphic --> Image

Code: Select all

convert box.jpg -trim -fuzz 4% box_trimmed.jpg
Here is the result of the code above --> Image

As you can see, the trim helped, but left an uneven border around the box. I would like to get ride of the entire white border. I've tried different combinations of fuzz and also have tried using blur.

I would greatly appreciate if someone can point out what I'm doing wrong. Thanks!

Re: Help! Poor results from Trim

Posted: 2013-01-06T17:22:59-07:00
by snibgo
For this image, "-fuzz 16%" seems to be required.

Re: Help! Poor results from Trim

Posted: 2013-01-06T17:36:26-07:00
by arnbrhm
Thanks for the quick response. Frustratingly, "-fuzz 16%" gives me the same result. I've given it a -fuzz 90% and it gives me the same result. Just to make sure it is writing the file, I've deleted the _trimmed version before running the new trim command. Why would it work for you but not me?

Also, i'm looking for a solution that will work on all of my postcards (of varying sizes). Does this mean I would have to adjust the -fuzz each time? That is kind of hard to automate.

Re: Help! Poor results from Trim

Posted: 2013-01-06T18:51:59-07:00
by snibgo
What version of IM are you on? If you are running from a batch file, are you remembering to double-up the percent sign?

Re: Help! Poor results from Trim

Posted: 2013-01-06T19:01:49-07:00
by fmw42
Put the -fuzz before the -trim

convert box.jpg -fuzz 4% -trim box_trimmed.jpg

You may also need a larger fuzz value since you are using jpg for the input.

Re: Help! Poor results from Trim

Posted: 2013-01-06T19:05:36-07:00
by arnbrhm
I just downloaded/installed it today. Here is what I got from the convert.exe properties --> ImageMagick 6.8.1-6 2012-12-29 Q16

I am running from a batch file, and was only using a single percent sign. Adding the double percent did not change anything. Just to be sure, I also ran the command from a command prompt with single and double percent signs.

I appreciate your suggestions. Do you have any more? I have tested this on a Windows 7 and XP SP3 machine.

Re: Help! Poor results from Trim

Posted: 2013-01-06T19:35:04-07:00
by fmw42
try putting the fuzz value before the trim

As you are on windows, you need to escape the % as %%

convert.exe box.jpg -fuzz 4%% -trim box_trimmed.jpg

I tested it and you need to get to about -fuzz 30%% to get it to work, since your input is jpg, it is not a uniform red on white.

Re: Help! Poor results from Trim

Posted: 2013-01-06T19:36:25-07:00
by arnbrhm
fmw42 wrote:Put the -fuzz before the -trim

convert box.jpg -fuzz 4% -trim box_trimmed.jpg

You may also need a larger fuzz value since you are using jpg for the input.
I was afraid it was a simple mistake like this. Thank you for pointing this out. Like I said, I'm very new to Image Magick. Changing the location of -fuzz did fix my test image (the red box). I'm trying these on a large number of my files and I'll report back if I have any further issues.

Many Thanks!

Re: Help! Poor results from Trim

Posted: 2013-01-06T19:39:13-07:00
by fmw42
Note my comment above about needing to increase your -fuzz value to about 30% to work properly on this jpg. You can test for your best value.

Re: Help! Poor results from Trim

Posted: 2013-01-06T19:47:50-07:00
by snibgo
As fmw42 says, the order is significant. IM generally wants the commands in a "logical", sequential order. Open an image, set up the fuzz factor for trimming, then trim, and finally save the image.

Different versions of IM need different fuzz factors. My usual version (6.7.9-6) needs 16%. V6.8.0-9 needs 21% to fully remove the white border.

In batch files, Windows tries to interpret "%" as the start of a variable, so it must be doubled if it isn't a variable (or, confusingly, even if it is, sometimes).