Help! Poor results from Trim

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
arnbrhm
Posts: 4
Joined: 2013-01-06T16:41:10-07:00
Authentication code: 6789

Help! Poor results from Trim

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help! Poor results from Trim

Post by snibgo »

For this image, "-fuzz 16%" seems to be required.
snibgo's IM pages: im.snibgo.com
arnbrhm
Posts: 4
Joined: 2013-01-06T16:41:10-07:00
Authentication code: 6789

Re: Help! Poor results from Trim

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help! Poor results from Trim

Post 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?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Help! Poor results from Trim

Post 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.
arnbrhm
Posts: 4
Joined: 2013-01-06T16:41:10-07:00
Authentication code: 6789

Re: Help! Poor results from Trim

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

Re: Help! Poor results from Trim

Post 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.
Last edited by fmw42 on 2013-01-06T19:38:02-07:00, edited 1 time in total.
arnbrhm
Posts: 4
Joined: 2013-01-06T16:41:10-07:00
Authentication code: 6789

Re: Help! Poor results from Trim

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

Re: Help! Poor results from Trim

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Help! Poor results from Trim

Post 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).
snibgo's IM pages: im.snibgo.com
Post Reply