简体   繁体   English

Python 无法从图像中读取文本 [Python OCR with Tesseract]

[英]Python cannot read text from an image [Python OCR with Tesseract]

I have this issue with reading exactly two lines of numbers (each line contains max of 3 digits) from an image.我在从图像中准确读取两行数字(每行最多包含 3 位数字)时遇到了这个问题。

My Python code has a big problem with reading a data from images like the ones below:我的 Python 代码在从如下图像中读取数据时存在很大问题:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

Most of the times it is just printing random numbers.大多数时候它只是打印随机数。 What should I do to make this work?我应该怎么做才能完成这项工作?

This is my Python code:这是我的 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)

The image has to have white text on the black background or the black text on the white background.图像必须在黑色背景上有白色文本或在白色背景上有黑色文本。

It is also important to save the image afterwards.之后保存图像也很重要。

Tesseract works best on images having black text on white Background. Tesseract 在白色背景上有黑色文本的图像上效果最好。 Invert the image before using tesseract by adding the below line:通过添加以下行,在使用 tesseract 之前反转图像:

 convertedScreenshot = 255 - convertedScreenshot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM