Pytesseract OCR multiple config options

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

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/