Command line image manipulation

Dear Experts,

Is it possible via command line to rotate an image automatically so it is perfectly straight?  For example, lets say I scan in a document and the scan is slightly at an angle because the document was not inserted into the scanner properly.  I know you can rotate the image using something like this:

convert -rotate 90 input.jpg output.jpg

But is it possible to automatically detect how much it needs to be rotated by to straighten it up?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There is a way to have it rotate if the size meets criteria x.

See here.
narmi2Author Commented:
I can't figure out from that link how the program can automatically detect how much to rotate the scanned document to straighten it up.
I'm not sure how automatic it is based on your needs but the section that may be of use is the following.

as it says if you add -rotate -"90>" it will rotate and image if the width is more than the height. It's only automatic based on the criteria of either width less than height (>) or if width is less than height.

-rotate degrees{<}{>}
Apply Paeth image rotation (using shear operations) to the image.  

Use > to rotate the image only if its width exceeds the height. < rotates the image only if its width is less than the height. For example, if you specify -rotate "-90>" and the image size is 480x640, the image is not rotated. However, if the image is 640x480, it is rotated by -90 degrees. If you use > or <, enclose it in quotation marks to prevent it from being misinterpreted as a file redirection.

Empty triangles in the corners, left over from rotating the image, are filled with the background color.

See also the -distort operator and specifically the 'ScaleRotateTranslate' distort method.
narmi2Author Commented:
Here is an example scan image I want to straighten up

As you can see, this is slightly tilted.  Can the above commands straighten this up without me telling it how much to straighten it up by?
narmi2Author Commented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.