Automatically deskew and crop scans

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
Smasher
Posts: 2
Joined: 2020-02-03T03:36:22-07:00
Authentication code: 1152

Automatically deskew and crop scans

Post by Smasher »

Hi altogether,

I want to automatically process a bunch of scanned images. Here I have an example:

Image

To process it manually, I rotate it by -90.395 degrees and crop to a desired area. The outcome looks like this:

Image

Can I achieve the same result with ImageMagick, so that it automatically determines the rotation angle and area to be cropped to?

User avatar
fmw42
Posts: 26269
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Automatically deskew and crop scans

Post by fmw42 »

ImageMagick cannot determine that the image is rotated by about 90 degrees. But it can deskew the small rotation. try

Code: Select all

convert image.jpg -deskew 40% result.png
If you know that it needs to be rotated also by 90, then

Code: Select all

convert image.jpg -deskew 40% -fuzz 45% -trim +repage -rotate -90 result.png
Or if you want to trim any extra white border and can live with some image being trimmed then try:

Code: Select all

convert img.jpg -deskew 40% -fuzz 8% -define trim:percent-background=0% -trim +repage -rotate -90 result.png
If using ImageMagick 7, then change convert to magick

Smasher
Posts: 2
Joined: 2020-02-03T03:36:22-07:00
Authentication code: 1152

Re: Automatically deskew and crop scans

Post by Smasher »

Hi fmw42 and sorry for taking too long to give you feedback. I tried all three variants of parametrization. The best result is achieved with

Code: Select all

-deskew 40% -fuzz 45% -trim +repage -rotate -90
This is how it looks like:
Image
As you see there is some extra white border remained, which you perfectly predicted :) But unfortunately, the last command doesn't fix it. Actually its result is even worse:
Image
I would stay happy with your second suggestion but since I want to apply it to a bunch of files, I have to check how it handles all of them. I execute the following
command:

Code: Select all

find . -name "*.tif" -exec convert {} -deskew 40% -fuzz 45% -trim +repage result/'{}'.png \;
And most of the results don't look satisfying :? Too much white border is left on the left here for example:
Image
Or on the top here:
Image

User avatar
fmw42
Posts: 26269
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Automatically deskew and crop scans

Post by fmw42 »

Did you try

Code: Select all

convert img.jpg -deskew 40% -fuzz 8% -define trim:percent-background=0% -trim +repage -rotate -90 result.png
Adjust the fuzz value as desired.

You might try my script, unrotate2, if you are using a Unix-based system (Linux, Mac OSX, Windows 10 Unix or Windows w/Cygwin)

Post Reply