简体   繁体   English

使用Python Tesseract从图像获取文本,但出现错误

[英]Using Python Tesseract to get text from image, but getting an error

I'm attempting to use to Python Tesseract to get text fron an image on my macos desktop and am running into an error that I cannot figure out. 我正在尝试使用Python Tesseract在macOS桌面上从图像获取文本,并且遇到了无法解决的错误。 I'm running macos High Sierra 10.3.2 我正在运行macOS High Sierra 10.3.2

My directory is set to my desktop (where the image lives) and I already specified the path to my tesseract executable. 我的目录设置为桌面(图像所在的位置),并且已经指定了tesseract可执行文件的路径。

I'm running 我在跑

print(pytesseract.image_to_string(Image.open('test.png')) 

and getting the following error: 并得到以下错误:

File "/Users/name/anaconda2/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 140, in run_and_get_output
    run_tesseract(**kwargs)
  File "/Users/name/anaconda2/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 116, in run_tesseract
    raise TesseractError(status_code, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (1, u'File "/var/folders/cp/dg2snlxn2631h8jx1bwb7jk80000gn/T/tess_cK4lka.PNG", line 1 SyntaxError: Non-ASCII character \'\\x89\' in file /var/folders/cp/dg2snlxn2631h8jx1bwb7jk80000gn/T/tess_cK4lka.PNG on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details')

Any idea what might be causing this and how to get around it? 知道是什么原因造成的,如何解决? Would be happy to provide any clarifying details. 很乐意提供任何澄清的细节。

Thanks! 谢谢!

Seems like you are trying to render a Non-ASCII character. 似乎您正在尝试呈现非ASCII字符。 Try adding this to the top of your .py file to ensure UTF-8 encoding: 尝试将其添加到.py文件的顶部,以确保UTF-8编码:

# -*- coding: utf-8 -*- 

As stated by the error message, see this for more details. 如错误消息所述,请参阅以获取更多详细信息。

User the unidecode library 使用unidecode库

from unidecode import unidecode
    .
    .
    .
    print unidecode(pytesseract.image_to_string(Image.open('test.png')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM