Fix Image Orientation
IronOCR’s OcrInput
includes built-in methods to adjust image orientation for optimal OCR accuracy:
Rotate(double degrees)
: rotates the image clockwise by the given degrees; use negative values for counter-clockwise rotationsDeskew(int maxDeskewAngle = 45)
: corrects skew up to a maximum angle (default 45°); returnstrue
if correction was appliedScale(int percent, bool scaleCrop = true)
: resizes the image proportionally (e.g. 150 for 150%)
using IronOcr;
using System;
class OrientationCorrectionExample
{
static void Main()
{
using var ocr = new IronTesseract();
using var input = new OcrInput();
// Load an input image
input.AddImage("example.jpg");
// Detect and log current orientation if needed:
var orientations = input.DetectPageOrientation();
foreach (var r in orientations)
{
Console.WriteLine($"Page {r.PageNumber}: rotated {r.RotationAngle}°, confidence: {r.HighConfidence}");
if (!r.HighConfidence)
Console.WriteLine("Warning: low confidence in orientation detection.");
}
// 1. Rotate by 90°
input.Rotate(90);
// 2. Deskew if necessary (default max 45°)
bool didDeskew = input.Deskew();
Console.WriteLine($"Deskew applied: {didDeskew}");
// 3. Scale to 150%
input.Scale(150);
// (Optional) save filtered image for inspection
input.SaveAsImages("processed_images");
// OCR the final corrected image
var result = ocr.Read(input);
Console.WriteLine("OCR text:");
Console.WriteLine(result.Text);
}
}
using IronOcr;
using System;
class OrientationCorrectionExample
{
static void Main()
{
using var ocr = new IronTesseract();
using var input = new OcrInput();
// Load an input image
input.AddImage("example.jpg");
// Detect and log current orientation if needed:
var orientations = input.DetectPageOrientation();
foreach (var r in orientations)
{
Console.WriteLine($"Page {r.PageNumber}: rotated {r.RotationAngle}°, confidence: {r.HighConfidence}");
if (!r.HighConfidence)
Console.WriteLine("Warning: low confidence in orientation detection.");
}
// 1. Rotate by 90°
input.Rotate(90);
// 2. Deskew if necessary (default max 45°)
bool didDeskew = input.Deskew();
Console.WriteLine($"Deskew applied: {didDeskew}");
// 3. Scale to 150%
input.Scale(150);
// (Optional) save filtered image for inspection
input.SaveAsImages("processed_images");
// OCR the final corrected image
var result = ocr.Read(input);
Console.WriteLine("OCR text:");
Console.WriteLine(result.Text);
}
}
Imports IronOcr
Imports System
Friend Class OrientationCorrectionExample
Shared Sub Main()
Dim ocr = New IronTesseract()
Dim input = New OcrInput()
' Load an input image
input.AddImage("example.jpg")
' Detect and log current orientation if needed:
Dim orientations = input.DetectPageOrientation()
For Each r In orientations
Console.WriteLine($"Page {r.PageNumber}: rotated {r.RotationAngle}°, confidence: {r.HighConfidence}")
If Not r.HighConfidence Then
Console.WriteLine("Warning: low confidence in orientation detection.")
End If
Next r
' 1. Rotate by 90°
input.Rotate(90)
' 2. Deskew if necessary (default max 45°)
Dim didDeskew As Boolean = input.Deskew()
Console.WriteLine($"Deskew applied: {didDeskew}")
' 3. Scale to 150%
input.Scale(150)
' (Optional) save filtered image for inspection
input.SaveAsImages("processed_images")
' OCR the final corrected image
Dim result = ocr.Read(input)
Console.WriteLine("OCR text:")
Console.WriteLine(result.Text)
End Sub
End Class
Explanation
- Loading the Image:
AddImage("example.jpg")
loads the input intoOcrInput
--similar toImage.open(...)
in PIL. DetectPageOrientation()
: returns orientation information per page, includingRotationAngle
andHighConfidence
flags (useful before applying corrections)- Rotation:
Rotate(90)
rotates the image clockwise by 90 degrees—matching PIL’srotate(degrees, expand=True)
behavior. - Deskewing:
Deskew()
automatically straightens minor skew. Returnstrue
if the image was corrected. - Scaling:
Scale(150)
scales the image proportionally to 150% the original dimensions—similar to resizing in the provided Python PIL example. - Optional Export:
SaveAsImages("processed_images")
exports filtered images for debugging or verification before OCR. - OCR Execution:
ocr.Read(input)
uses the preprocessedOcrInput
for text extraction.