Install pytesseract on Windows
First download tesseract and install it
https://github.com/UB-Mannheim/tesseract/wiki
pip install pytesseract
References
https://pypi.org/project/pytesseract/
https://github.com/UB-Mannheim/tesseract/wiki
First download tesseract and install it
https://github.com/UB-Mannheim/tesseract/wiki
pip install pytesseract
References
https://pypi.org/project/pytesseract/
https://github.com/UB-Mannheim/tesseract/wiki
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/
import cv2 image = cv2.imread("ocr.png") image_grayscal = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) inverted_image=cv2.bitwise_not(image_grayscal)
References
https://stackoverflow.com/questions/19580102/inverting-image-in-python-with-opencv
os.execl(sys.executable, sys.argv[0], *sys.argv)
The asterisk in the command is a wildcard that should not be taken literally.
The “l” and “v” variants of the exec* functions differ in how command-line arguments are passed.
References
https://stackoverflow.com/questions/11329917/restart-python-script-from-within-itself
https://docs.python.org/3/library/os.html#os.execl