I've provided a Colab solution since that will probably be useful to the most people.
Install tesseract in our Colab environment.
!sudo apt install tesseract-ocr
!pip install PyTesseract
Import libraries and mount our Drive
from google.colab import drive
from google.colab.patches import cv2_imshow
drive.mount('/content/drive/')
Set our pytesseract path, read in source image from our Drive, show our source image, then finally convert the image to text.
import cv2
import pytesseract
import numpy as np
pytesseract.pytesseract.terreract_cmd = {
r'/usr/bin/tesseract'
}
src = cv2.imread('/content/drive/MyDrive/Colab Notebooks/images/macbeth.png')
cv2_imshow(src)
output_txt = pytesseract.image_to_string(src)
print(type(output_txt))
print(output_txt)

What is the Best Way to Get Text From Image with Python? - Stack Overflow
Just a quick question. Any idea on how to extract text from an image file?
Image to text python - Stack Overflow
for the life of me, i can't figure out how to extract text from images. how can i get this done without paying for anything?
Im using pyautogui to navigate my screen. Ill be setting the screenshot area to pretty small predetermined areas that will have random numbers. Need a way to convert snipped image to text. Any advice is greatly appreciated
You have to have tesseract installed and accesible in your path.
According to source, pytesseract is merely a wrapper for subprocess.Popen with tesseract binary as a binary to run. It does not perform any kind of OCR itself.
Relevant part of sources:
def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False, config=None):
'''
runs the command:
`tesseract_cmd` `input_filename` `output_filename_base`
returns the exit status of tesseract, as well as tesseract's stderr output
'''
command = [tesseract_cmd, input_filename, output_filename_base]
if lang is not None:
command += ['-l', lang]
if boxes:
command += ['batch.nochop', 'makebox']
if config:
command += shlex.split(config)
proc = subprocess.Popen(command,
stderr=subprocess.PIPE)
return (proc.wait(), proc.stderr.read())
Quoting another part of source:
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'
So quick way of changing tesseract path would be:
import pytesseract
pytesseract.tesseract_cmd = "/absolute/path/to/tesseract" # this should be done only once
pytesseract.image_to_string(img)
Please install the Below packages for extracting text from images pnf/jpeg
pip install pytesseract
pip install Pillow
using python pytesseract OCR (Optical Character Recognition) is the process of electronically extracting text from images
PIL is used anything from simply reading and writing image files to scientific image processing, geographical information systems, remote sensing, and more.
from PIL import Image
from pytesseract import image_to_string
print(image_to_string(Image.open('/home/ABCD/Downloads/imageABC.png'),lang='eng'))