Home > Mobile >  Python convert color image to black text on white background for OCR
Python convert color image to black text on white background for OCR

Time:07-18

I have an image that need to do OCR (Optical Character Recognition) to extract all data.

enter image description here

First I want to convert color image to black text on white background in order to improve OCR accuracy.

I try below code

from PIL import Image
img = Image.open("data7.png")
img.convert("1").save("result.jpg")

it gave me below unclear image

enter image description here

I expect to have this image

enter image description here

Then, I will use pytesseract to get a dataframe

import pytesseract as tess
file = Image.open("data7.png")
text = tess.image_to_data(file,lang="eng",output_type='data.frame')
text

Finally,the dataframe I want to get like below

enter image description here

CodePudding user response:

You can extract the background color by looking at the most prominent color while measuring the input image statistics with Torchvision.

More specifically you can use enter image description here

Then you can extract the data frame using enter image description here

You still need to figure out how to capture the text in colors, but the noise is gone once you turn off dithering.

  • Related