Deskew not working, is it me?

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
pr3sidentspence
Posts: 27
Joined: 2018-10-31T12:29:16-07:00
Authentication code: 1152

Deskew not working, is it me?

Post by pr3sidentspence » 2018-10-31T14:16:00-07:00

I'm trying to deskew scans of vintage postcards on a black cardstock background (so not pure black) and ImageMagick's deskew is not working for me at any fuzziness level. I've also tried Fred's unrotate script with similar non-results. Sometimes both deskew and unrotate think the image needs zero rotation, sometimes they rotate the image but make it more skewed not less, occasionally it will get close but not to the best possible (not even within 5 degrees) and that might just be a coincidence.

Challenges with the working set of scans:
  • old postcards are rarely rectangular. They can have corners that are 90 +/- 2 degrees. It's possible that none of the edges will be parallel with another.

    like a Polaroid, many have a border. In the past, imagemagick would detect this and crop out the edges of these cards

    The cards are mostly black & white, but some are sepia, some are colorized, and some are just color art.

    the cards are scanned on a black background, the shade of which is often found in the cards themselves.
However, I even made some test images with slightly rotated white or grey rectangles on black, or black rectangles on white, and both deskew and unrotate can't seem to straighten them.

Any suggestions?

Here is a sample image: https://imgur.com/4AwJQkj Imagehttps://imgur.com/4AwJQkj

Image

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

Re: Deskew not working, is it me?

Post by fmw42 » 2018-10-31T15:59:40-07:00

Try adding -background somecolor, where somecolor is the color outside the image in your background area. This works fine for me on IM 6.9.10.14 Q16 Mac OSX for you first image.

Code: Select all

convert image.jpg -background "gray(55)" -deskew 40% result.png
or

Code: Select all

convert image.jpg -background "gray(55)" -deskew 40% +repage -fuzz 20% -trim +repage result.png

pr3sidentspence
Posts: 27
Joined: 2018-10-31T12:29:16-07:00
Authentication code: 1152

Re: Deskew not working, is it me?

Post by pr3sidentspence » 2018-11-01T06:55:29-07:00

Thank you! I'll give that a try as soon as I reach the office.

pr3sidentspence
Posts: 27
Joined: 2018-10-31T12:29:16-07:00
Authentication code: 1152

Re: Deskew not working, is it me?

Post by pr3sidentspence » 2018-11-01T07:49:02-07:00

Hmm. I am starting to think it really is just me.

On ubuntu with ImageMagick 6.9.7-4 Q16 neither of the commands you provided produced a change. Neither did ImageMagick 7.0.4-8 Q16 on Windows 10.

pr3sidentspence
Posts: 27
Joined: 2018-10-31T12:29:16-07:00
Authentication code: 1152

Re: Deskew not working, is it me?

Post by pr3sidentspence » 2018-11-01T08:03:40-07:00

Oh ho! I had put a reduced resolution (25%) version of the scan on imgur just so it wouldn't be ridiculously large. That's not what I was testing on.

When I test on the one I put on imgur it works.

Is there a max size that deskew will work with?

EDIT: And if that's the case, is there a way to get deskew to just output the degree to which it would change the rotation by, which I could then apply to the full resolution image?

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

Re: Deskew not working, is it me?

Post by fmw42 » 2018-11-01T09:04:57-07:00

My experience is that -deskew only works for rotations of about 5 degrees or less.

pr3sidentspence
Posts: 27
Joined: 2018-10-31T12:29:16-07:00
Authentication code: 1152

Re: Deskew not working, is it me?

Post by pr3sidentspence » 2018-11-01T10:04:13-07:00

<5 degrees would be fine, I think my problem is the size of the original image. If I resize it down it works.

Is there a way to get deskew to output a degrees rotation needed like your unrotate does if you don't specify out outfile? I tried not specifying one using deskew but it threw an error. That way I could batch a resize down, run the deskew on it, get the number and then rotate the original fullsize.

pr3sidentspence
Posts: 27
Joined: 2018-10-31T12:29:16-07:00
Authentication code: 1152

Re: Deskew not working, is it me?

Post by pr3sidentspence » 2018-11-01T10:53:42-07:00

I think I found my answer:

Code: Select all

convert input.jpg -background "gray(55)" -deskew 40% -print '%[deskew:angle]\n' null:

pr3sidentspence
Posts: 27
Joined: 2018-10-31T12:29:16-07:00
Authentication code: 1152

Re: Deskew not working, is it me?

Post by pr3sidentspence » 2018-11-13T15:56:08-07:00

Hmm... it seems a large number of the later replies were lost somehow.

Regardless, here is my somewhat-final batch file that takes large images (BMP encoded in the batch file, but easy to change) that ImageMagick failed to deskew for me and:
  1. makes a smaller working copy
  2. figures out how much the copy needs rotating and assigns that to a variable
  3. -rotate the original using that variable while also converting it to 100% jpeg
  4. performs a -trim on this, and tosses away the result, but takes the -verbose output
  5. Pads the trim coordinates and size from (4) with extra pixels
  6. -crops using the padded values to make a border using the scanned background.
(print %@ might have been easier to parse than -verbose, but I was having trouble with %@ on my dev computer)

Post Reply