【问题标题】:Python cannot read text from an image [Python OCR with Tesseract]Python 无法从图像中读取文本 [Python OCR with Tesseract]
【发布时间】:2020-05-31 16:14:22
【问题描述】:

我在从图像中准确读取两行数字(每行最多包含 3 位数字)时遇到了这个问题。

我的 Python 代码在从如下图像中读取数据方面存在很大问题:

大多数时候它只是打印随机数。 我应该怎么做才能完成这项工作?

这是我的 Python 代码:

from PIL import ImageGrab, Image
from datetime import datetime
from pytesseract import pytesseract
import numpy as nm


pytesseract.tesseract_cmd = 'F:\\Tesseract\\tesseract'

while True:
    screenshot = ImageGrab.grab(bbox=(515, 940, 560, 990))
    datetime = datetime.now()
    filename = 'pic_{}.{}.png'.format(datetime.strftime('%H%M_%S'), datetime.microsecond / 500000)

    gray = screenshot.convert('L')
    bw = nm.asarray(gray).copy()

    bw[bw < 160] = 0
    bw[bw >= 160] = 255

    convertedScreenshot = Image.fromarray(bw)

    tesseract = pytesseract.image_to_string(convertedScreenshot, config='digits --psm 6')

    convertedScreenshot.save(filename)

    print(tesseract)

图片必须是黑底白字或白底黑字。

事后保存图片也很重要。

【问题讨论】:

    标签: python python-3.x ocr tesseract python-tesseract


    【解决方案1】:

    Tesseract 最适用于在白色背景上有黑色文本的图像。在使用 tesseract 之前通过添加以下行来反转图像:

     convertedScreenshot = 255 - convertedScreenshot
    

    【讨论】:

    • 没有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多