Change language

Python | Using PIL ImageGrab and PyTesseract

Installing —

Pillow (a newer version of PIL)

PyTesseract

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 ()

Exit

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

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically