I have an image that need to do OCR (Optical Character Recognition) to extract all data.
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
I expect to have this image
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
Here's a vanilla Pillow solution. Just grayscaling the image gives us okay results, but the green text is too faint.
So, we first scale the green channel up (sure, it might clip, but that's not a problem here), then grayscale, invert and auto-contrast the image.
from PIL import Image, ImageOps
img = Image.open('rqDRe.png').convert('RGB')
r, g, b = img.split()
img = Image.merge('RGB', (
r,
g.point(lambda i: i * 3), # brighten green channel
b,
))
img = ImageOps.autocontrast(ImageOps.invert(ImageOps.grayscale(img)), 5)
img.save('rqDRe_processed.png')