2017-07-01 66 views
0

我正在編寫一個python腳本以使用pytesseract從文件中提取圖像。但是當我運行腳本時,我得到了下面的錯誤。無法使用pytesseract從圖像中提取文本

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') 

錯誤接收:

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' 

可否請你指出我,我在這裏做了錯誤。

回答

0

終於明白了......我沒有在我的工作站上安裝tesseract庫,因爲上面的代碼不工作。確保安裝tesseract庫和相應的依賴項以便能夠使用pytesseract。