我已经开始在 Python 中使用 pytesserract。当我在
中传递单引号或双引号时from PIL import Image
import pytesseract
import numpy as np
tesseract_config = r"""-c tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#'<>(){};:"""
tesseract_language = "eng"
text = pytesseract.image_to_string(Image.open('res/outc001.jpg'), lang=tesseract_language, config=tesseract_config)
print text
返回
Traceback (most recent call last):
File "main.py", line 15, in <module>
text = pytesseract.image_to_string(Image.open('res/outc001.jpg'), lang=tesseract_language, config=tesseract_config).split('\n')
File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 193, in image_to_string
return run_and_get_output(image, 'txt', lang, config, nice)
File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 140, in run_and_get_output
run_tesseract(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 106, in run_tesseract
command += shlex.split(config)
File "/usr/lib/python2.7/shlex.py", line 279, in split
return list(lex)
File "/usr/lib/python2.7/shlex.py", line 269, in next
token = self.get_token()
File "/usr/lib/python2.7/shlex.py", line 96, in get_token
raw = self.read_token()
File "/usr/lib/python2.7/shlex.py", line 172, in read_token
raise ValueError, "No closing quotation"
ValueError: No closing quotation
我一直在寻找一种方法来转义单引号和双引号,但都没有用。
当我运行 tesseract 时
tesseract res/outc001.jpg tesseract_out/out001 -c "tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#\'\"<>(){};:"
它工作得很好。
最佳答案
Pytesseract 使用 shlex分隔配置参数。
shlex 的转义符是\
, 如果你想在 shlex.split()
中插入引号函数你必须用 \
转义它.
如果你想要
'
仅在白名单中:tesseract_config = "-c tessedit_char_whitelist=blahblah\\'")
如果你想要
"
仅:tesseract_config = '-c tessedit_char_whitelist=blahblah\\"')
如果你想要两者
'
和"
:tesseract_config = '''-c tessedit_char_whitelist=blahblah\\'\\"''')
或
tesseract_config = """-c tessedit_char_whitelist=blahblah\\"\\'""")
关于python - pytesseract tessedit_char_whitelist 不接受报价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49587228/