Can I auto-rotate an image that contains mainly text? Maybe via OCR? The algorithm or whatever needs to scan the image and decide if it has to rotate it 90°, 180° or 270°

I want to include this feature into an existing PowerShell script, so command-line ability is mandatory.

I'm open for ImageMagick solutions


enter image description here
Click to enlarge

I have already read the following SO questions. But they rotate photos.
and I'm only trying to rotate pure text scans which have no EXIF metadata.


I currently do this with tesseract using the switches "-psm 0" which will detect orientation of the image.

Sample output :

Tesseract Open Source OCR Engine v3.04.00 with Leptonica
Orientation: 3
Orientation in degrees: 90
Orientation confidence: 3.94
Script: 1
Script confidence: 13.81

Then I run ImageMagick to rotate the image to the correct orientation. It does work for most of the images, except for handwritten documents and photos.

A bit kludgy, but it is a lot faster than running OCR four times.


You want the ImageMagick convert utility. Just run

convert -rotate 90 image.png

and it will be rotated 90 degrees and saved back as the same file name.

  • Well, I thought it is obvious from the question (» OCR). Apparently not. Sometimes the scans must be rotated 270°, 90° or 180°. That is not fixed – nixda Mar 19 '14 at 19:58
  • @nixda There is also an -auto-orient option that may be of use - I haven't used it, so I don't know how accurate it is. Sorry, I didn't pick up from your original question that they weren't all in the same orientation. – MattDMo Mar 19 '14 at 20:02
  • The documentation says it's using EXIF data. So this is no solution. If the EXIF profile was previously stripped, the -auto-orient operator will do nothing. – nixda Mar 20 '14 at 9:27

Not the most elegant way, but you could try to OCR in all four orientations and whichever one has the least amount of gibberish is the correct one. I did some quick searching, and I found a few references which discuss using such a technique, for example:

Phase two of document correction is the contextual auto-rotate. Using a full-page OCR read at several orientations the software can determine at which orientation the quality of the read is best. This is the most accurate way to rotate a document. Documents with little text, or text at various angles are the only risky documents. In these cases, the software chooses the orientation of the MOST readable text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.