How to Fix Image Colors for Reading

by Chaknith Bin

Fixing image colors involves several techniques to improve the legibility and quality of an image. IronOcr offers binarization, grayscale, inversion, and color replacement methods to make text and content within an image more readable and aesthetically pleasing, which is particularly important when working with OCR (Optical Character Recognition) to extract text from images. Reading only the selected text color is also possible.

Get started with IronOCR

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

First Step:
green arrow pointer



Binarize Image Example

This process converts the image into a two-color format, typically black and white. It's useful for separating text from the background and reducing noise, making the text more distinct and easier to read.

To apply the binarization effect to the image, use the Binarize method. Since OCR processes work best with the highest contrast image, featuring black text on a white background, this method proves significant in making the background very distinctive from the characters.

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

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

// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Apply binarize affect
imageInput.Binarize();

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

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Apply binarize affect
imageInput.Binarize()

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

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

Sample image
Binarized image

Grayscale Image Example

Converting the image into various shades of gray can make it less distracting and more reader-friendly. This is especially helpful when the original colors in the image are causing visual clutter.

To apply the grayscale effect to the image, use the ToGrayScale method. The grayscale process involves taking the average of the R, G, and B values.

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-grayscale-image.cs
// Apply grayscale affect
imageInput.ToGrayScale();
' Apply grayscale affect
imageInput.ToGrayScale()
VB   C#
Sample image
Grayscaled image

Invert Image Example

Inverting the colors can enhance contrast. For example, turning white text on a black background into black text on a white background can improve readability.

Use the Invert method to invert the image color. The method optionally accepts a boolean value, which is used to remove all color channels and return a grayscale image.

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-invert-image.cs
// Apply invert affect
imageInput.Invert();
' Apply invert affect
imageInput.Invert()
VB   C#

The below images show the Invert method with and without the grayscale option.

Inverted image
Inverted and grayscaled image

Replace Color Example

This technique allows you to replace specific colors in the image with other colors, which can help highlight or de-emphasize certain elements. It's often used to make text more prominent or to correct problematic color contrasts.

To use the ReplaceColor method, specify the current color to be replaced as well as the new color. The third parameter of the method, which corresponds to the tolerance value, is also important. A higher tolerance is required in blurry images to achieve the desired result.

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

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

// Add image
using var imageInput = new OcrImageInput("sample.jpg");
IronSoftware.Drawing.Color currentColor = new IronSoftware.Drawing.Color("#DB645C");
IronSoftware.Drawing.Color newColor = IronSoftware.Drawing.Color.DarkCyan;

// Replace color
imageInput.ReplaceColor(currentColor, newColor, 80);

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

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("sample.jpg")
Private currentColor As New IronSoftware.Drawing.Color("#DB645C")
Private newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan

' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)

' Export the modified image
imageInput.SaveAsImages("replaceColor")
VB   C#
Sample image
Replaced color image

Read Specific Text Color Example

This feature aims to read only the specified text color. Use the SelectTextColor method to specify the color for IronOcr to focus on, along with the tolerance value. The tolerance value accepts a range of 0-255, which represents the allowable difference between pixel color and the selected color for each R, G, and B value in the color space.

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-select-text-color.cs
using IronOcr;
using System;

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

// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Text color to focus on
IronSoftware.Drawing.Color focusColor = new IronSoftware.Drawing.Color("#DB645C");

// Specify which text color to read
imageInput.SelectTextColor(focusColor, 60);

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output result to console
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Text color to focus on
Private focusColor As New IronSoftware.Drawing.Color("#DB645C")

' Specify which text color to read
imageInput.SelectTextColor(focusColor, 60)

' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Output result to console
Console.WriteLine(ocrResult.Text)
VB   C#

Below, you will find the OCR result, which is intended to read only the text in an orange-ish color.

OCR result
Chaknith related to Read Specific Text Color Example

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.