从 python 中的特定图像块中读取文本

Read text from particular block of image in python

有没有办法从特定固定位置的图像中读取文本?

def read_image_data(request):
    import cv2
    import pytesseract
    pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract-OCR/tesseract.exe"
    img = cv2.imread("workcenter_dash.png")
    text = pytesseract.image_to_string(img)
    print(text)

在上面的示例中,我使用“pytesseract”来读取图像文本,它可以很好地读取文本,但在我的例子中,我想从特定位置读取文本。

例如:在上图中,我只想从用红色矩形选中的位置读取文本。

所以请给我最好的解决方案。 提前致谢。

我通过裁剪图像然后在读取其数据后解决了这个问题。

它对我有用,如果对此问题有更好的解决方案,请给我建议。

def read_image_data(request):
    import cv2
    import pytesseract
    pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract-OCR/tesseract.exe"
    img = cv2.imread("workcenter_dash.png")
    
    height, width = img.shape[0:2]
    startRow = int(height * 0.10)
    startCol = int(width * 0.10)
    endRow = int(height * 0.90)
    endCol = int(width * 0.40)

    croppedImage = img[startRow:endRow, startCol:endCol]
    text = pytesseract.image_to_string(croppedImage)
    print(text)