How to Fix Image Orientation for Reading

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.

Get started with IronOCR

Start using IronOCR in your project today with a free trial.

First Step:
green arrow pointer



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;

// The IronTesseract class provides advanced OCR capabilities, allowing
// you to recognize text from images.
var ocrTesseract = new IronTesseract();

// The OcrInput class is used to define input images for OCR processing.
// Here, we're specifying an image file for input.
using var ocrInput = new OcrInput("paragraph_skewed.png");

// Logically rotate the image by 180 degrees using the Rotate method.
// This prepares the image for OCR by ensuring it's in the correct orientation.
ocrInput.Rotate(180);

// Save the rotated image to a new file. To correctly save the modified image,
// we use the SaveAsImages method which accepts a directory name and saves 
// the images within that directory.
ocrInput.SaveAsImages("rotated_images_directory");

// Perform OCR on the rotated image.
// The Read method processes the image and returns the results.
var result = ocrTesseract.Read(ocrInput);

// Output the recognized text to the console.
Console.WriteLine(result.Text);

// Note: The Read method reads the OcrInput and recognizes the text,
// which is stored in the OcrResult object and then outputted.
Imports IronOcr

' The IronTesseract class provides advanced OCR capabilities, allowing
' you to recognize text from images.
Private ocrTesseract = New IronTesseract()

' The OcrInput class is used to define input images for OCR processing.
' Here, we're specifying an image file for input.
Private ocrInput = New OcrInput("paragraph_skewed.png")

' Logically rotate the image by 180 degrees using the Rotate method.
' This prepares the image for OCR by ensuring it's in the correct orientation.
ocrInput.Rotate(180)

' Save the rotated image to a new file. To correctly save the modified image,
' we use the SaveAsImages method which accepts a directory name and saves 
' the images within that directory.
ocrInput.SaveAsImages("rotated_images_directory")

' Perform OCR on the rotated image.
' The Read method processes the image and returns the results.
Dim result = ocrTesseract.Read(ocrInput)

' Output the recognized text to the console.
Console.WriteLine(result.Text)

' Note: The Read method reads the OcrInput and recognizes the text,
' which is stored in the OcrResult object and then outputted.
$vbLabelText   $csharpLabel

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
using System;

public class ImageProcessor
{
    // A placeholder for the actual image data
    // Replace 'byte[]' with the appropriate data type for your image data (e.g. Bitmap, Image)
    private byte[] _imageData;

    // Constructor initializing the image data
    public ImageProcessor(byte[] imageData)
    {
        if (imageData == null || imageData.Length == 0)
        {
            throw new ArgumentNullException(nameof(imageData), "Image data cannot be null or empty.");
        }
        _imageData = imageData;
    }

    // Method to apply deskew operation on the image
    public void Deskew()
    {
        // Actual deskewing operation implementation would go here
        // Example: Adjusts the image to correct any misalignment

        // For demonstration purposes, simply outputting to console
        Console.WriteLine("Deskew operation applied.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Example image data
        // Replace this with the actual image data type for your application
        byte[] sampleImageData = new byte[100]; // Assuming your image data type is byte[]

        // Creating an instance of the ImageProcessor class with sample image data
        ImageProcessor imageInput = new ImageProcessor(sampleImageData);

        // Apply deskew operation on the image
        imageInput.Deskew();
    }
}
Imports System

Public Class ImageProcessor
	' A placeholder for the actual image data
	' Replace 'byte[]' with the appropriate data type for your image data (e.g. Bitmap, Image)
	Private _imageData() As Byte

	' Constructor initializing the image data
	Public Sub New(ByVal imageData() As Byte)
		If imageData Is Nothing OrElse imageData.Length = 0 Then
			Throw New ArgumentNullException(NameOf(imageData), "Image data cannot be null or empty.")
		End If
		_imageData = imageData
	End Sub

	' Method to apply deskew operation on the image
	Public Sub Deskew()
		' Actual deskewing operation implementation would go here
		' Example: Adjusts the image to correct any misalignment

		' For demonstration purposes, simply outputting to console
		Console.WriteLine("Deskew operation applied.")
	End Sub
End Class

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Example image data
		' Replace this with the actual image data type for your application
		Dim sampleImageData(99) As Byte ' Assuming your image data type is byte[]

		' Creating an instance of the ImageProcessor class with sample image data
		Dim imageInput As New ImageProcessor(sampleImageData)

		' Apply deskew operation on the image
		imageInput.Deskew()
	End Sub
End Class
$vbLabelText   $csharpLabel
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
// <summary>
// The code below demonstrates an example of an 'Image' class that contains a method to scale an image.
// The 'Scale' method is called on an 'Image' object with a specified scale factor as an argument.
// This example assumes that `imageInput` is a valid object with a method `Scale`.
// </summary>

using System;

// Assuming there is a class Image with a Scale method that accepts a scale percentage
public class Image
{
    // Assuming 'ScaleFactor' is the storage for the current scaling percentage of the image.
    private double scaleFactor = 100;
    
    // Scales the image by the given percentage
    public void Scale(double percentage)
    {
        // Verify the percentage is a valid scale value
        if (percentage <= 0)
        {
            throw new ArgumentException("Scale percentage must be greater than zero.");
        }

        // Apply scaling logic
        scaleFactor = percentage;

        // Output a message to indicate scaling has been applied
        Console.WriteLine($"Image scaled to {scaleFactor}% of its original size.");
    }
}

// Top-level statement to create and scale an Image object
// Initialize a new Image object
Image imageInput = new Image();

// Apply a scale to the image using the Scale method
imageInput.Scale(70);

// Note: This example shows a simple implementation of an Image class with a Scale method.
// Adjust this code to fit the actual context and implementations of your application.
' <summary>
' The code below demonstrates an example of an 'Image' class that contains a method to scale an image.
' The 'Scale' method is called on an 'Image' object with a specified scale factor as an argument.
' This example assumes that `imageInput` is a valid object with a method `Scale`.
' </summary>

Imports System

' Assuming there is a class Image with a Scale method that accepts a scale percentage
Public Class Image
	' Assuming 'ScaleFactor' is the storage for the current scaling percentage of the image.
	Private scaleFactor As Double = 100

	' Scales the image by the given percentage
	Public Sub Scale(ByVal percentage As Double)
		' Verify the percentage is a valid scale value
		If percentage <= 0 Then
			Throw New ArgumentException("Scale percentage must be greater than zero.")
		End If

		' Apply scaling logic
		scaleFactor = percentage

		' Output a message to indicate scaling has been applied
		Console.WriteLine($"Image scaled to {scaleFactor}% of its original size.")
	End Sub
End Class

' Top-level statement to create and scale an Image object
' Initialize a new Image object
Private imageInput As New Image()

' Apply a scale to the image using the Scale method
imageInput.Scale(70)

' Note: This example shows a simple implementation of an Image class with a Scale method.
' Adjust this code to fit the actual context and implementations of your application.
$vbLabelText   $csharpLabel

Size comparison

Size comparison
Size comparison

Frequently Asked Questions

What is image orientation correction?

Image orientation correction involves adjusting an image to ensure it is properly aligned for purposes such as text recognition. This includes techniques like rotation, deskewing, and scaling.

How does IronOCR help in image orientation correction?

IronOCR provides methods to fix image orientation by supporting rotation, deskewing, and scaling, which are essential for preparing images for accurate text recognition.

How can I rotate an image using IronOCR?

You can rotate an image using the Rotate method in IronOCR. Pass a degree value to rotate the image clockwise or counterclockwise.

What is deskewing and how is it applied?

Deskewing is the process of straightening a tilted or skewed image. It is applied using the Deskew method, which corrects any slant or misalignment to ensure horizontal alignment.

How do I scale an image using IronOCR?

To scale an image in IronOCR, use the Scale method with a percentage value. You can also specify whether associated crop areas should be scaled proportionally.

Can IronOCR handle PDF documents for image orientation correction?

Yes, IronOCR can import and process PDF documents for image orientation correction, allowing for adjustments like rotation, deskewing, and scaling.

What is the advantage of using IronOCR for text recognition?

IronOCR ensures text is correctly oriented, aligned, and appropriately sized for extraction, leading to improved accuracy in text recognition.

Is it possible to export images after correction in IronOCR?

Yes, after applying image orientation corrections, you can export the modified images using the SaveAsImages method in IronOCR.

What is the impact of using higher deskew values in IronOCR?

Higher deskew values may provide more correction opportunities but can result in slower processing and an increased risk of errors, such as upside-down pages.

Why is it important to correct image orientation before text recognition?

Correcting image orientation ensures that text is properly aligned and sized, leading to more accurate text extraction and recognition during OCR processing.

Chaknith related to Size comparison
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.