尝试将页面分割模式 (PSM) 设置为模式 6,这会将 OCR 设置为检测单个统一的文本块。
具体来说,这样做:
bal = pytesseract.image_to_string(balIm, config='--psm 6')
这应该可以满足您的需求。事实上,我尝试在你的图像上运行它,它给了我想要的东西。请注意,我首先下载了您在上面提供的图像,并在我的本地计算机上离线读取了图像:
In [8]: import pytesseract
In [9]: from PIL import Image
In [10]: balIm = Image.open('wC62s.png')
In [11]: pytesseract.image_to_string(balIm, config='--psm 6')
Out[11]: '0.03,'
作为最后一点,如果您发现 Tesseract 开箱即用并不适合您,请考虑尝试其中一种页面分割模式来帮助提高准确性:https://tesseract-ocr.github.io/tessdoc/ImproveQuality#page-segmentation-method。为了完整起见,我将在下面提供给您。
0 Orientation and script detection (OSD) only.
1 Automatic page segmentation with OSD.
2 Automatic page segmentation, but no OSD, or OCR.
3 Fully automatic page segmentation, but no OSD. (Default)
4 Assume a single column of text of variable sizes.
5 Assume a single uniform block of vertically aligned text.
6 Assume a single uniform block of text.
7 Treat the image as a single text line.
8 Treat the image as a single word.
9 Treat the image as a single word in a circle.
10 Treat the image as a single character.
11 Sparse text. Find as much text as possible in no particular order.
12 Sparse text with OSD.
13 Raw line. Treat the image as a single text line,
bypassing hacks that are Tesseract-specific.
当您运行image_to_string 时,指定一个输入参数config,它接受您要在其中操作的 PSM。尝试其中的一些,直到您让它适用于您的图像。确保在执行之前在config 参数中使用--psm。