如何使用pytesseract将图像转换为python中的数字

2024-06-26 09:26:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用pytesseract将图像转换为字符串/整数。唯一的问题是每次我运行代码时都没有发生任何事情。我将图像更改为文本图像,读取“text”,pytesseract很好地检测到了它。下面是我用来将图像转换成字符串的内容。我还包括了我一直在使用的图像

NumberTest

bal = pytesseract.image_to_string(balIm) print(bal)

我不知道还有什么可以尝试的。我唯一能想到的是尝试另一种OCR,任何帮助都将不胜感激,谢谢


Tags: to字符串代码text图像image文本内容
1条回答
网友
1楼 · 发布于 2024-06-26 09:26:07

尝试将页面分割模式(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,'

作为最后的注释,如果你看到TestSerAt对你来说不是很有效,请考虑尝试他们的页面分割模式来帮助提高准确性: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

相关问题 更多 >