使用python突出显示图像中的特定文本 [英] Highlighting specific text in an image using python

查看:27
本文介绍了使用python突出显示图像中的特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想突出显示网站截图中的特定单词/句子。

截图之后,我使用pytesseractcv2提取文本。它工作得很好,我可以获得有关它的文本和数据。

import pytesseract
import cv2


if __name__ == "__main__":
    img = cv2.imread('test.png')
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    result = pytesseract.image_to_data(img, lang='eng', nice=0, output_type=pytesseract.Output.DICT)
    print(result)

使用Results对象,我可以找到所需的单词和句子。

问题是如何返回图像并突出显示这些单词?

我是否应该查看其他库,或者是否有办法获取像素值,然后突出显示文本?

理想情况下,我希望获得每个单词的开始和结束坐标,如何才能做到这一点?

推荐答案

您可以使用pytesseract.image_to_boxes方法获取图像中标识的每个字符的边框位置。如果需要,还可以使用该方法在某些特定字符周围绘制边界框。下面的代码在我标识的图像周围绘制矩形。

import cv2
import pytesseract
import matplotlib.pyplot as plt

filename = 'sf.png'

# read the image and get the dimensions
img = cv2.imread(filename)
h, w, _ = img.shape # assumes color image

# run tesseract, returning the bounding boxes
boxes = pytesseract.image_to_boxes(img)use
print(pytesseract.image_to_string(img)) #print identified text

# draw the bounding boxes on the image
for b in boxes.splitlines():
    b = b.split()
    cv2.rectangle(img, ((int(b[1]), h - int(b[2]))), ((int(b[3]), h - int(b[4]))), (0, 255, 0), 2)

plt.imshow(img)

这篇关于使用python突出显示图像中的特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆