Clearing edges

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
JohnS
Posts: 5
Joined: 2016-04-29T10:08:44-07:00
Authentication code: 1151

Clearing edges

Post by JohnS »

Hello,

First I've got to say I'm completely amazed by this program, it's absolutely incredible.

I've got to scan a few thousand papers. Some are all color, some are text only. The scanner I'm using is a fast 200 ipm feeder, however on some of the sheets I'm getting black lines where the paper isn't completely even along the edges.

I've got to detect if there are black gaps on an image (basically vertical black regions along the sides or horizontal along the top and bottom), and then deal with them somehow. Is there a means to programatically detect that, then trim those parts automatically using ImageMagick?

I've been looking into Morphology, but I can't do a universal action without first detecting to see if it's even needed. If it makes any difference I'm on Windows. Also, I'm not looking for someone to create anything for me, just to point me in a direction.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clearing edges

Post by fmw42 »

Please, always identify your IM version and platform, since syntax may differ.

Can you post an example image? You can upload to some free hosting service such as dropbox.com and put the URL here.
JohnS
Posts: 5
Joined: 2016-04-29T10:08:44-07:00
Authentication code: 1151

Re: Clearing edges

Post by JohnS »

I'm running 6.9.3-Q16 on Windows 7 64bit.

https://www.dropbox.com/sh/hps7qawht302 ... oZhNa?dl=0

All of the pages are considered sensitive in some nature. However I left the problem areas untouched. Basically I'd be pre-cropping the headers off the pages, usually resulting in a square area that needs more work done to it.

Thanks for your time, really it's appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clearing edges

Post by fmw42 »

Unfortunately, these little images with large color blocks will not help us to understand your problem. If they are proprietary, then you could send the links to one or two in a PM to me on the forum. I do not use Windows. I am a Mac person (unix), so I can give you my suggestions and perhaps try a few things, but you may want a Windows person to help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clearing edges

Post by snibgo »

You want to trim black edges from all four sides.

The following will trim black from the left edge, putting the result in x1.png. For the other edges, first rotate by 90 degrees, than by -90 degrees at the end. It is trivial to do the following four times, but this can be optimised to just two converts per image.

Windows BAT syntax.

Code: Select all

set SRC=3.jpg

for /F "usebackq tokens=3 delims=x+" %%L in (`%IM%convert ^
  %SRC% ^
  -scale "x1^!" ^
  -bordercolor Black ^
  -border 1 ^
  -fuzz 95%% -format "%%@" ^
  info:`) do set /A DX=%%L-1

echo DX=%DX%

%IM%convert %SRC% -crop +%DX%+0 +repage x1.png
It works by averaging the image to a single row. Then it finds how many pixels from the left are above 95% of white. That number is put in DX. Then it trims DX pixels from the source image.

With this 95%, the left edge of the output will be at least 95% white. You may need to adjust the percentage.
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: Clearing edges

Post by fmw42 »

Snibgo's solution of averaging down to one row or column is the same as what I had thought might be needed. So, since he uses Windows, I will let him continue with you.
JohnS
Posts: 5
Joined: 2016-04-29T10:08:44-07:00
Authentication code: 1151

Re: Clearing edges

Post by JohnS »

That works wonders. I never knew you could do so much with bat files. Thank you so much, this has gotten me really excited about Imagemagick.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clearing edges

Post by snibgo »

I said:
For the other edges, first rotate by 90 degrees, than by -90 degrees at the end.
Of course, I meant rotate by 90 or 180 or 270 degrees, then rotate back at the end.

My pages contain many examples of using ImageMagick to do wonderful things in BAT files.
snibgo's IM pages: im.snibgo.com
JohnS
Posts: 5
Joined: 2016-04-29T10:08:44-07:00
Authentication code: 1151

Re: Clearing edges

Post by JohnS »

I've been trying to understand exactly what's going on in this script. Is there some site that can break down the two lines that start with -fuzz and info: ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clearing edges

Post by snibgo »

Code: Select all

-fuzz 95%% -format "%%@" info:
"info:" writes text information, as defined by "-format". We say what text information we want with "%@". (Windows BAT syntax needs us to double the percent.)

For the percent-escapes, see http://www.imagemagick.org/script/escape.php . "%@" gives the rectangle that would be found if we did a "-trim". "-fuzz 95%" means we treat any pixels within 95% of the border as the border, so it would be trimmed away.

For descriptions of "-fuzz" etc, see http://www.imagemagick.org/script/comma ... s.php#fuzz

Does that answer the question?
snibgo's IM pages: im.snibgo.com
JohnS
Posts: 5
Joined: 2016-04-29T10:08:44-07:00
Authentication code: 1151

Re: Clearing edges

Post by JohnS »

So basically, the bat works as follows :

SRC is declared as our source file name

Then the for basically creates a black 1px border, then a rectangle region that encompasses pixels that are within a percentage of the border color and sets that as %L, then sets %DX as %L-1.

Then the crop command is sent.

That's still a bit hazy to me. Unfortunately I don't have much experience with BAT scripting, so the syntax is extremely cryptic to me.

I want to apologize for being a pain, I'm just trying to wrap my mind around this. Thank you again for your time and experience, I really appreciate it.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clearing edges

Post by snibgo »

Almost.

The IM "convert" command creates a black border around the entire image, and calculates the effect of a trim. The output from "%@" is four integers in the form "WxH+X+Y", being the width, height and offsets from top-left of the resulting rectangle. The "for" construct extracts the third number, the X-offset, into %L. This number is one greater than it should be (because we added a border thickness 1), so we subtract 1 from %L before putting the value in DX.

We use that value to crop DX pixels from the left side of SRC.

The "echo" isn't needed, of course, it just tells you what it's doing.

Script languages are mostly cryptic.
snibgo's IM pages: im.snibgo.com
Post Reply