Page 1 of 1

Deskew option makes output image smaller than original

Posted: 2014-02-17T19:49:57-07:00
by redhwan
I'm trying to correct some image alignment issues given a scanned copy of a form. I've received some good results using the deskew option but am facing a situation whereby the output image becomes condensed than the original image. Not in terms of image width and height (as I've set them in my program to resize to 880 by 1165) though.

This is a concern as I've defined (manually) certain (x,y) coordinates on the original form to act as control points, and the decrease in resolution throws it off. Is there a way I can use the deskew option without this happening?

I've attached sample images for your reference.

Side by Side Comparisonhttp://kratmat.s3.amazonaws.com/sample_form_chart.jpg

Original Images
http://kratmat.s3.amazonaws.com/input.jpg
http://kratmat.s3.amazonaws.com/output.jpg
http://kratmat.s3.amazonaws.com/original.jpg

Notice that in the first link, the output image differs slightly from the original image when compared.. I need it to match exactly as the original image.

Appreciate any help rendered. Thank you!

Re: Deskew option makes output image smaller than original

Posted: 2014-02-17T20:23:21-07:00
by fmw42
The issue is that -deskew will unrotate to the same size as its input. The result will be shrunk and padded with white to fill the input size. Unfortunately, there is no +deskew to produce the properly rotated output size. But to correct that what you can do is deskew and get the rotation angle. Then use -distort SRT to do the rotation.

see %[deskew:angle] at http://www.imagemagick.org/script/escape.php

and

see -distort SRT at http://www.imagemagick.org/Usage/distorts/#srt

Unfortunately, I am temporarily on an older computer with an old IM version that does not allow deskew:angle. But give the following a try

This is Linux/Mac syntax. If on Windows, someone else will have to help convert this for you.

Code: Select all

angle=`convert input.jpg -format "%[deskew:angle]" info:`
size=`convert input.jpg -format "%wx%h" info:`
convert input.jpg +distort SRT "1 $angle" -gravity center -crop $size+0+0 +repage result.jpg

Re: Deskew option makes output image smaller than original

Posted: 2014-02-18T03:40:44-07:00
by redhwan
Hi thanks so much for your reply.

I'm getting:

Code: Select all

convert input.jpg -format "%[deskew:angle]" info:
convert: unknown image property "%[deskew:angle]" @ warning/property.c/InterpretImageProperties/3403.
Do you perhaps mean this instead?

Code: Select all

convert input.jpg -deskew 40 -format '%[deskew:angle]' info:
-3.14419
There appears to be an error returned for the last line:

Code: Select all

convert input.jpg +distort SRT "1 $angle" -gravity center -crop $size+0+0 +repage result.jpg
convert: invalid argument for option `-crop': convert @ error/convert.c/ConvertImageCommand/1125.
On the matter as well, could you explain to me more of the last line? I'm not sure if I understand it fully. Why do I need to include -gravity center? And $size+0+0 - what is the 0+0 indicative of? The offset?

Re: Deskew option makes output image smaller than original

Posted: 2014-02-18T10:50:58-07:00
by fmw42
convert input.jpg -deskew 40 -format '%[deskew:angle]' info:
-3.14419

There appears to be an error returned for the last line:

convert input.jpg +distort SRT "1 $angle" -gravity center -crop $size+0+0 +repage result.jpg
convert: invalid argument for option `-crop': convert @ error/convert.c/ConvertImageCommand/1125.
Yes on the first one. I was on an old version and did not remember the method.

On the second, try

Code: Select all

angle=`convert input.jpg -deskew 40 -format "%[deskew:angle]" info:`
size=`convert input.jpg -format "%wx%h" info:`
convert input.jpg +distort SRT "1 $angle" -gravity center -crop ${size}+0+0 +repage result.jpg
The gravity center will make the crop pick the middle of the image so you do not get too much pad at the top and left. The +0+0 is the offset relative to the gravity.

see
http://www.imagemagick.org/Usage/crop/#crop

Re: Deskew option makes output image smaller than original

Posted: 2014-02-20T09:24:28-07:00
by redhwan
Thanks so much, Fred, that explains a lot. I ran this on another form, and there is a small issue, though. The output image gets pushed extremely to the left.

Is there any way I can add a fixed padding (in pixels) at the top, left, bottom and right of the form? Say around 80px inwards?

Not sure if I'm explaining well - do let me know if you need further clarifications.

Re: Deskew option makes output image smaller than original

Posted: 2014-02-20T10:27:15-07:00
by fmw42
add -bordercolor white -border 80 to the end of the command just before the output. or leave off the -gravity, but it may crop too much on the right and bottom.

or better

Code: Select all

angle=`convert input.jpg -deskew 40 -format "%[deskew:angle]" info:`
size=`convert input.jpg -format "%wx%h" info:`
convert input.jpg +distort SRT "1 $angle" -fuzz XX% -trim +repage -bordercolor white -border 80 result.jpg
try with xx=0 and increase as needed

see
http://www.imagemagick.org/Usage/crop/#border
http://www.imagemagick.org/Usage/crop/#trim

Re: Deskew option makes output image smaller than original

Posted: 2014-02-21T02:05:58-07:00
by redhwan
Thanks Fred, I will try this and let you know again in due course. I appreciate your patience.

Re: Deskew option makes output image smaller than original

Posted: 2014-02-25T18:58:38-07:00
by redhwan
Hi Fred,

Just checking in to update as promised. I kept the original command, and modified it slightly, moving the crop command to end last and adding a blue border (which is cropped later).

Seems to work to suit my purpose. Thanks again for your patience!

Code: Select all

convert [file_name] +distort SRT 1,$angle -gravity center -bordercolor blue -border 150x0 +repage -crop $img_dimensions+0+0 [filename]