我有一幅图像,上面有我用PyTesseract扫描过的数字列表来构造一个字符串。具体来说,这是代码:
from PIL import Image
import pytesseract
from scipy import stats
import numpy as np
pytesseract.pytesseract.tesseract_cmd = r'C:\\\Program Files\\\Tesseract-OCR\\\tesseract.exe'
str1=pytesseract.image_to_string(Image.open('D:/Image.png'))
这是我正在扫描的图像:
问题在于PyTesseract将图像扫描为单个字符而不是整数。
我想了解为什么会这样,我该怎么做才能获得理想的结果。
简而言之,PyTesseract不会扫描数字列表中的整数,而是将它们扫描为单个字符。我如何告诉它扫描整数并将其放入数组中?
答案 0 :(得分:1)
好吧,如果只想获取列表,请使用re.split
和strip
来解决。(因为tesseract的结果有一些错误)。
您可以尝试以下方法:
import pytesseract
import re
data = pytesseract.image_to_string('OCR.png')
dataList = re.split(r',|\.| ',data) # split the string
resultList = [int(i.strip()) for i in dataList if i != ''] # remove the '' str and convert str to int.
print(resultList)
# result: [71, 194, 38, 1701, 89, 76, 11, 83, 1629, 48, 94, 63, 132, 16, 111, 95, 84, 341, 975, 14, 40, 64, .......