Search code examples
pythonopencvocrtesseractpython-tesseract

Binarize an image and extract a text where the background is black and the text to be extract is red?


I'm trying extract text from a screenshot using cv2 and pytesseract in memory.

When the text is white over a black background it works, but when the text is red the return is always empty.

import sys
from PIL import ImageGrab
import cv2
import numpy as np
from pytesseract import pytesseract

pytesseract.tesseract_cmd = r'C:\site-packages\Tesseract-OCR\tesseract.exe'

def Extract(tupleCoordenates):

pic = ImageGrab.grab(bbox=tupleCoordenates)
img = np.array(pic)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
limiar, imgThreash = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
text = pytesseract.image_to_string(imgThreash)
return text

PS: this code below works when i save the pic on disk and get it from cv2.imread("myimage.jpeg", 0)

myImg = cv2.imread("myimage.jpeg", 0)    
limiar, imgThreash = cv2.threshold(myImg, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
cv2.imwrite(args[2], imgThreash)

I believe that the problem is the parse from pic -> img -> gray scale


Solution

  • I finally found a solution to my problem.

    Follows the code:

    def ThresholdFromScreenShot(tupleCoordenates):
    
    pixels = np.array(ImageGrab.grab(bbox=tupleCoordenates))
    
    gray_f = np.array(Image.fromarray(pixels).convert('L'))
    
    limiar, imgThreash = cv2.threshold(gray_f, 127, 255, 
    cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    
    gray_s = np.array(Image.fromarray(imgThreash).convert('L'))
                
    blur = cv2.blur(gray_s,(3,3))
    
    limiar,thresh = cv2.threshold(blur,240,255,cv2.THRESH_BINARY)
        
    return thresh