I have been trying for hours to resize and change color of this image but nothing is consistently getting correct letters. Please see image below. This is a test image I'm using. The goal is to use this for automation purposes. Thanks!
Larger sample
import numpy as np
import pytesseract
from PIL import ImageGrab
import win32gui
import time
toplist, winlist = [], []
#time.sleep(3)
def enum_cb(hwnd, results):
if 'FPS:' in win32gui.GetWindowText(hwnd):
print(hex(hwnd), win32gui.GetWindowText(hwnd))
winlist.append(hwnd)
win32gui.EnumWindows(enum_cb, None)
win32gui.SetForegroundWindow(winlist[0])
bbox = win32gui.GetWindowRect(winlist[0])
print(bbox)
img = np.array(ImageGrab.grab(bbox=(130, 810, 800, 1080)))
#percent by which the image is resized
scale_percent = 400
#calculate the 50 percent of original dimensions
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
# dsize
dsize = (width, height)
# resize image
output = cv2.resize(img, dsize)
#img.show()
pytesseract.pytesseract.tesseract_cmd = r"L:\Program Files\Tesseract-OCR\tesseract.exe"
# img = cv2.imread(r"L:\MGO2PC\MGO2 UNOFFICIAL PC\RPCS3 EMU\screenshots\screenshot-2021_04_06_17_13_03.png", 0)
#crop_img = img[800:900, 260:800]
#cv2.imshow("cropped", crop_img)
#cv2.waitKey(0)
i = cv2.imwrite("test.png",output)
text = pytesseract.image_to_string(output, lang='eng')
print(text)
Ultimately found that I can isolate the text color and tesseract had no problem reading after that.
def cv2_from_screen(self):
boundaries = [
([0, 179, 105], [38, 255, 167]) # BGR
]
pytesseract.pytesseract.tesseract_cmd = r"L:\Program Files\Tesseract-
OCR\tesseract.exe"
def enum_cb(hwnd, results):
if 'FPS:' in win32gui.GetWindowText(hwnd):
print(hex(hwnd), win32gui.GetWindowText(hwnd))
self.winlist.append(hwnd)
win32gui.EnumWindows(enum_cb, None)
win32gui.EnumWindows(enum_cb, None)
win32gui.SetForegroundWindow(self.winlist[0])
image = pyautogui.screenshot()
image = cv2.cvtColor(np.array(image.crop(box=[0, 800, 1000, 1080])), cv2.COLOR_RGB2BGR) #COLOR_RGB2BGR and COLOR_BGR2GRAY