Python: Extract text from images with background

Viewed 89

I need to extract text from various screenshot, this is the code I'm using right now:

from PIL import Image
from pytesseract import pytesseract
 

path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image_path = r"screenshot_5850.jpg"
 

img = Image.open(image_path)
 
pytesseract.tesseract_cmd = path_to_tesseract
 
text = pytesseract.image_to_string(img, lang="ita")
 
print(text)

My problem is that the result I'm getting is not accurate at all, and I think the problem is the background of my image: the text I'm interested in is always white with black border, but the background change in every screenshot and can be various colours. These are a couple of examples of the image I'm working with:

enter image description here

enter image description here

How can I improve my code? thank you all

1 Answers

As @Stefano pointed out, some further image manipulation might be necessary.

Using PIL, you can simply call img.convert("1") to convert PIL Images to a black and white version.

See this article.

Edit:

Since you pointed out that this approach doesn't work, I would try to use PIL to get it to be as black and white as possible. There's a million different filters that you could apply with PIL.

If that doesn't work, the most extreme approach would be to iterate over all the pixels in the image, test if they are white, and if they aren't, set them to black. This would work since your text is always white, although any purely white areas of the background would be saved, so hopefully pytesseract can handle that.

# The extreme approach, keeping only white pixels
img = Image.open("c://path/to/file.jpg")
pixels = img.load() # create the pixel map

for i in range(img.size[0]):    # for every col:
    for j in range(img.size[1]):    # For every row
        # if the pixel isn't white, set it to black
        if pixels[i][j] != (255,255,255):
            pixels[i][j] = (0,0,0)
img.show()

Again, this is an extreme approach. You should play around with filters and your pytesseract settings (with which I'm unfamiliar) and try to get something easier to work first.

Related