【问题标题】:How to extract text or numbers from images using python如何使用python从图像中提取文本或数字
【发布时间】:2020-03-26 05:08:12
【问题描述】:

我想从这样的图像中提取文本(主要是数字)

我试过这段代码

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = Image.open('1.jpg')
text = pytesseract.image_to_string(img, lang='eng')
print(text)

但我得到的只是这个 (hE PPAR)

【问题讨论】:

    标签: python image ocr tesseract python-tesseract


    【解决方案1】:

    在执行 OCR 时,对图像进行预处理非常重要,以便要检测的所需文本为黑色,背景为白色。为此,这里有一个简单的方法,使用 OpenCV 对 Otsu 的图像进行阈值处理,这将产生二值图像。这是预处理后的图像:

    我们使用--psm 6 配置设置将图像视为统一的文本块。这是您可以尝试的其他configuration options。 Pytesseract 的结果

    01153521976

    代码

    import cv2
    import pytesseract
    
    pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    
    image = cv2.imread('1.png', 0)
    thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
    print(data)
    
    cv2.imshow('thresh', thresh)
    cv2.waitKey()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多