1

I need to use python tesseract to extract text from a photo:

import pytesseract
from PIL import Image


img = Image.open('stest.png')
pytesseract.pytesseract.tesseract_cmd = 'D:\\python\\venv\\Scripts\\pytesseract.exe'

file_name = img.filename
file_name = file_name.split(".")[0]


text = pytesseract.image_to_string(img,lang=None, config='')
print(text)

with open(f'{file_name}.txt', 'w') as text_file:
    text_file.write(text)

But the error appears as if it is not related to my code:

 Traceback (most recent call last):
  File "D:\python\pybotavito\main.py", line 13, in <module>
    text = pytesseract.image_to_string(img,lang=None, config='')
  File "D:\python\venv\lib\site-packages\pytesseract\pytesseract.py", line 413, in image_to_string
    return {
  File "D:\python\venv\lib\site-packages\pytesseract\pytesseract.py", line 416, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "D:\python\venv\lib\site-packages\pytesseract\pytesseract.py", line 284, in run_and_get_output
    run_tesseract(**kwargs)
  File "D:\python\venv\lib\site-packages\pytesseract\pytesseract.py", line 260, in run_tesseract
    raise TesseractError(proc.returncode, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (2, 'Usage: pytesseract [-l lang] input_file')
mark
  • 11
  • 2

1 Answers1

0

From the README page of pytesseract,

# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
# Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'

This line must point to the tesseract executable, NOT to the pytesseract python executable. Try to set the correct executable path. There is an example on where to find the executable in the comment bellow the corresponding line.

hamdanal
  • 477
  • 3
  • 10