PyTesseract 在 table 中看不到一些个位数

PyTesseract not seeing some single-digit numbers in table

我有这张图片 table

我正在尝试使用 PyTesseract 解析它。我已经非常接近使用这段代码了:

from PIL import Image, ImageOps
import pytesseract

og_image = Image.open('og_image.png')
grayscale = ImageOps.grayscale(og_image)
inverted = ImageOps.invert(grayscale.convert('RGB'))
print(pytesseract.image_to_string(inverted))

这似乎是非常准确的,除了倒数第二列中的个位数是空白。我需要做一些不同的事情来获取这些数字吗?

Tesseract有多种分页模式,选择合适的分页模式才能获得最佳效果。参见 documentation

同样在这种情况下,您可以将tesseract限制为特定字符集。

另一件事,tesseract 对字体和图像大小很敏感。一个简单的调整大小可以大大改变结果。在这里,我将图像大小水平更改为 2,垂直更改以获得最佳结果;)

结合以上所有内容,您将得到:

custom_config = r'--psm 6  -c tessedit_char_whitelist=0123456789.'
print(pytesseract.image_to_string(inverted.resize((1506, 412), Image.ANTIALIAS), config=custom_config))
      
1525 .199 303 82 161 162 7 .241
1464 .290 424 70 139 198 25 .352
1456 .292 425 116 224 224 0 .345
1433 .240 346 81 130 187 15 .275
1390 .273 373 108 217 216 3 .345
1386 .276 383 54 181 154 18 .315
1225 .208 255 68 148 129 1 .242
1218 .238 230 46 128 127 18 .273
1117 .240 268 43 113 1193 1 .308