2

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

Example

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.

4 Answers 4

3

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.

1

I'm also looking for a way to auto-rotate documents. Read the answer by @teikjoon, and wrote the following script using Tesseract and ImageMagick.

# orient_images '/b'
function orient_images() {
  for file in $1;
  do magick mogrify -rotate -$(tesseract $file - --psm 0 | sed -n 's/Rotate: \(.*\)/\1/p') $file;
  done;
}

This works, but now facing another issue where Tesseract gives low confidence scores on text orientation, so I'm looking into tools like Fred's TextCleaner script.

1

You can achieved that with ocrmypdf:

ocrmypdf --rotate-pages myfile.pdf myfile.pdf
1
  • works great! Thanks:)
    – sunew
    Commented Apr 9 at 20:12
0

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.

1
  • Which metric do you use to find the "most readable text" ?
    – tigrou
    Commented Jun 24 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .