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!
Deskew option makes output image smaller than original
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Deskew option makes output image smaller than original
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.
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
Hi thanks so much for your reply.
I'm getting:
Do you perhaps mean this instead?
There appears to be an error returned for the last line:
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?
I'm getting:
Code: Select all
convert input.jpg -format "%[deskew:angle]" info:
convert: unknown image property "%[deskew:angle]" @ warning/property.c/InterpretImageProperties/3403.
Code: Select all
convert input.jpg -deskew 40 -format '%[deskew:angle]' info:
-3.14419
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Deskew option makes output image smaller than original
Yes on the first one. I was on an old version and did not remember the method.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.
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
see
http://www.imagemagick.org/Usage/crop/#crop
Re: Deskew option makes output image smaller than original
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.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Deskew option makes output image smaller than original
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
try with xx=0 and increase as needed
see
http://www.imagemagick.org/Usage/crop/#border
http://www.imagemagick.org/Usage/crop/#trim
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
see
http://www.imagemagick.org/Usage/crop/#border
http://www.imagemagick.org/Usage/crop/#trim
Re: Deskew option makes output image smaller than original
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
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!
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]