Python | Using PIL ImageGrab and PyTesseract

Installing —

Pillow (a newer version of PIL)


Apart from this, a tesseract executable needs to be installed.

Code implementation

The following functions were mainly used in the code & # 8212 ;

pytesseract.image_to_string (image, lang = ** language **) – Takes the image and searches for words of the language in their text.

cv2.cvtColor (image, ** color conversion **) – Used to make the image monochrome (using cv2.COLOR_BGR2GRAY).

ImageGrab. grab (bbox = ** Coordinates of the area of ​​the screen to be captured **) – Used to repeatedly (using a loop) capture a specific part of the screen.

Objectives of the Code:

  1. Use a loop to capture multiple times part of the screen.
  2. To convert the captured image to grayscale.
  3. Use PyTesseract to read the text in it.

Code: Python code for using ImageGrab and PyTesseract

# cv2.cvtColor takes numy ndarray as an argument

import numpy as nm


import pytesseract

# OpenCV import

import cv2


from PIL import ImageGra b



def imToString ():


# Path to the tesseract executable

pytesseract.pytesseract.tesseract_cmd = `** Path to tesseract executable ** `

  while ( True ):


# ImageGrab-To capture an image on the screen in a loop.

# Bbox is used to capture the area.

cap = ImageGrab.grab (bbox = ( 700 , 300 , 1400 , 900 ))


# Convert the image to monochrome so it`s easy

# read the OCR and got the output string.

tesstr = pytesseract.image_to_string (

cv2.cvtColor (nm.array (cap), cv2.COLOR_BGR2GRAY), 

lang = ` eng` )

print (tesstr)

# Function call
imToString ()


The above code can be used to grab a specific section screen and read its text content.

Read about other libraries used in the code