Cut black edges off a book page scan

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
GrazingScientist
Posts: 3
Joined: 2018-08-08T22:14:26-07:00
Authentication code: 1152

Cut black edges off a book page scan

Post by GrazingScientist »

Hi everybody,

I am using ImageMagick 7.0.7-38 Q16 x86_64 on a MacOS 10.13.6.

I got thousands of book page scans like this, this and this. You will realize that all these book scans have a black border on 3 edges which varies in size.

I simply want to get rid of (most) of these edges and only get the page. Is this possible with IM?

I already tried things like:

Code: Select all

convert input.jpg -fuzz 10% -trim +repage out.jpg
or

Code: Select all

convert input.jpg -border 1x1 -bordercolor black -fuzz 10% -trim +repage out.jpg
(not perfectly sure on the commands here, but I think you get the idea. Also, the "10%" is only an example. I tried various numbers there.)
And also the MagicWand script from Fred, which is awesome but for a noob like me still a little too far away.

Do you see how I can remove the edges in the given images?

Thanks in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut black edges off a book page scan

Post by fmw42 »

Try my multicrop2 script. Use the smallest -f value and the largest -d value that will not remove all regions.


http://www.fmwconcepts.com/misc_tests/m ... 04019.jpeg

Code: Select all

multicrop2 -b "gray(60)" -f 75 -d 1000000  3704019.jpeg 3704019_result.jpg
http://www.fmwconcepts.com/misc_tests/m ... lt-000.jpg

There is going to be some remaining border. But you can use -shave afterwards to remove it.
GrazingScientist
Posts: 3
Joined: 2018-08-08T22:14:26-07:00
Authentication code: 1152

Re: Cut black edges off a book page scan

Post by GrazingScientist »

Your result looks amazing! o.O

I tried to reproduce this on my Mac but failed.
I downloaded the script, made it executeable, and applied it as you mentioned above (only with './multicrop2').
I can see that the script is working, because the terminal is busy doing stuff but outputs only three empty lines. When it's done, no result was produced in the same folder. I also tried giving aboslute paths of both input and output file but still nothing is happening. Do I need further scripts from your HP?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut black edges off a book page scan

Post by fmw42 »

I don't know what happened, but when I tried it again today, it did not work just as you described. I don't know why, but I must have tried -f 75 and thought it worked, but it is too large a value. The same command seems to work today using -f 50. Try that and let me know.
GrazingScientist
Posts: 3
Joined: 2018-08-08T22:14:26-07:00
Authentication code: 1152

Re: Cut black edges off a book page scan

Post by GrazingScientist »

The -f 50 did the trick! SIR! YOU! ARE! AMAZING! Thanks sooooo much!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut black edges off a book page scan

Post by fmw42 »

If you want to remove more of the black, since you have a large margin, you can follow up with the Imagemagick command -shave. See https://www.imagemagick.org/Usage/crop/#shave
Post Reply