How to Fix Image Orientation for Reading

by Chaknith Bin

Fixing image orientation, in the context of image processing, involves making adjustments to an image to ensure it is properly aligned for specific purposes, such as text recognition. IronOcr supports fixing image orientation, including rotation, deskewing, and scaling.

These techniques are essential for preparing images for accurate text recognition, as they ensure that text is correctly oriented, aligned, and appropriately sized for extraction.


C# NuGet Library for OCR

Install with NuGet

Install-Package IronOcr
or
C# OCR DLL

Download DLL

Download DLL

Manually install into your project

Rotate Image Example

Rotating an image involves changing its orientation by a specific angle (e.g., 90 degrees clockwise or counterclockwise) to ensure that the text or content within the image is upright and correctly aligned.

Pass a degree value to the Rotate method to perform the rotation. A positive degree value will rotate the image clockwise, while a negative degree value will rotate the image counterclockwise.

:path=/static-assets/ocr/content-code-examples/how-to/image-orientation-correction-rotate-image.cs
using IronOcr;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("paragraph_skewed.png");

// Rotate the image 180 degrees clockwise
imageInput.Rotate(180);

// Export the modified image
imageInput.SaveAsImages("rotate");
Imports IronOcr

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("paragraph_skewed.png")

' Rotate the image 180 degrees clockwise
imageInput.Rotate(180)

' Export the modified image
imageInput.SaveAsImages("rotate")
VB   C#

For convenience, you can export the modified image using the SaveAsImages method. Below is a comparison of the image before and after rotation.

Sample image
Rotated image

Deskew Image Example

Deskewing is the process of straightening an image that may be slightly tilted or skewed. It corrects any slant or misalignment, ensuring that the text or content appears horizontally aligned.

To apply deskewing to the image, use the Deskew method. This method accepts an integer value that specifies the maximum angle of skew to correct. Higher values may provide more opportunities for correction, but they can result in slower processing and an increased risk of errors, including upside-down pages.

:path=/static-assets/ocr/content-code-examples/how-to/image-orientation-correction-deskew-image.cs
// Apply deskew
imageInput.Deskew();
' Apply deskew
imageInput.Deskew()
VB   C#
Sample image
deskewed image

Scale Image Example

Scaling involves resizing an image to a specific dimension or aspect ratio. This can be useful to standardize image sizes for more consistent text recognition.

To apply scaling to the image, use the Scale method. The Scale method takes a percentage value, with 100% meaning no effect. The second parameter is the ScaleCropArea, which determines whether associated crop areas should also be scaled proportionally (recommended as 'true').

:path=/static-assets/ocr/content-code-examples/how-to/image-orientation-correction-scale-image.cs
// Apply scale
imageInput.Scale(70);
' Apply scale
imageInput.Scale(70)
VB   C#

Size comparison

Size comparison
Size comparison

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.