0

I am writing a python script to extract image from a file using pytesseract. But when I run the script I am getting the below error.

from PIL import Image
from pytesseract import image_to_string
import os

tessdata_dir_config = '--tessdata-dir "C:\\Ranit\\Personal\\PycharmProjects\\modlog\\pdfs\\"'

tgtfile = "C:\Ranit\Personal\PycharmProjects\modlog\pdfs\IMG_20170331_194534.jpg"

if os.path.isfile(tgtfile):
    imgtxt = image_to_string(image='IMG_20170331_194534.jpg',config=tessdata_dir_config)
    print(imgtxt)
else:
    print('File not found')

Error received:

C:\Python36\python.exe C:/Ranit/Personal/PycharmProjects/modlog/extracttext.py
Traceback (most recent call last):
  File "C:/Ranit/Personal/PycharmProjects/modlog/extracttext.py", line 10, in <module>
    imgtxt = image_to_string(image='IMG_20170331_194534.jpg',config=tessdata_dir_config)
  File "C:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 117, in image_to_string
    image.save(input_file_name)
AttributeError: 'str' object has no attribute 'save'

Could you please point me the error that I am doing here.

Ranit Das
  • 64
  • 1
  • 11

1 Answers1

1

Finally got it...I had not installed the tesseract library in my workstation due to which the above code was not working. Make sure to install the tesseract library and corresponding dependencies to be able to use pytesseract.

Ranit Das
  • 64
  • 1
  • 11