Pytesseract OCR multiple config options

Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
                        bypassing hacks that are Tesseract-specific.

References
https://stackoverflow.com/questions/44619077/pytesseract-ocr-multiple-config-options

Pytesseract not working sometimes on perfectly clear Images

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('2.jpg',0)
thresh = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY_INV)[1]

result = cv2.GaussianBlur(thresh, (5,5), 0)
result = 255 - result

data = pytesseract.image_to_string(result, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey()

We use the --psm 6 config flag since we want to treat the image as a single uniform block of text.

References
https://stackoverflow.com/questions/57719983/pytesseract-not-working-sometimes-on-perfectly-clear-images
https://stackoverflow.com/questions/44619077/pytesseract-ocr-multiple-config-options

Read text in image using pytesseract

from PIL import Image
import pytesseract

im = Image.open("sample1.jpg")

text = pytesseract.image_to_string(im, lang = 'eng')

print(text)

Or

import pytesseract
import cv2

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'

image = cv2.imread("ocr.png")
image_grayscal = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

data = pytesseract.image_to_string(image_grayscal)

Read texts in white color

import pytesseract
import cv2

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'

image = cv2.imread("ocr.png")
image_grayscal = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
inverted_image = cv2.bitwise_not(image_grayscal) # color of text is white so we should invert colors

data = pytesseract.image_to_string(inverted_image)

References
https://stackoverflow.com/questions/50951955/pytesseract-tesseractnotfound-error-tesseract-is-not-installed-or-its-not-i
https://pupli.net/2020/05/inverting-colors-of-image-in-python-with-opencv/