pytesseract tessedit_char_whitelist不接受引用

时间:2018-03-31 11:29:14

标签: python tesseract quotes python-tesseract pytesser

我已经开始在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#\'\"<>(){};:"

它运作得很好。

1 个答案:

答案 0 :(得分:2)

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