【自动化】pytesseract获取指定文字在图片中的坐标,并裁剪文字所在的区域

示例效果

要从如下图片中查找指定文字“清君侧”的具体坐标,并裁剪文字所在的区域
在这里插入图片描述
实际输出如下:
在这里插入图片描述
裁剪保存的图片如下:
在这里插入图片描述

具体代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from PIL import Image
import pytesseract


def find_substring_indicds(str_dict, word):
    """
    滑动窗口思想查找指定字符串
    :param str_dict: 从图片中提取的字符轮廓字典数据
    :param word: 待查找的字符串
    :return:
    """
    dict_num = len(str_dict['char'])
    word_num = len(word)
    start = 0
    end = 0
    while end < dict_num:
        str = "".join(str_dict['char'][start:end+1])
        if str == word:
            print([start, end])
            return start, end
        elif len(str) < word_num:
            end += 1
        else:
            start += 1
    return False


# 打开图片转灰度
img = Image.open('input/纯文本-黑白3.png').convert('L')
# 识别多行文本
custom_oem_psm_config = r'--oem 2 --psm 6'

# 提取图片中的字符轮廓数据,以字典格式返回
# 要注意 pytesseract.image_to_boxes 的坐标原点在图片的左下角(确实有点不习惯)
boxes_dict = pytesseract.image_to_boxes(img, config=custom_oem_psm_config, lang='chi_sim', output_type='dict')
print(boxes_dict)

start, end = find_substring_indicds(boxes_dict, '清君侧')

print(f"char:{''.join(boxes_dict['char'][start:end+1])}")
left_1 = boxes_dict['left'][start]
top_1 = boxes_dict['top'][start]
bottom_1 = boxes_dict['bottom'][start]
left_2 = boxes_dict['left'][end]
top_2 = boxes_dict['top'][end]
bottom_2 = boxes_dict['bottom'][end]
x_center = (left_1 + left_2)/2
y_center = (top_1 + bottom_2)/2

print(f"以下坐标的坐标原点为左下角\n坐标1:{left_1} {top_1} {bottom_1}")
print(f"坐标2:{left_2} {top_2} {bottom_2}")
print(f"图片尺寸:{img.size}")
print(f"x_center:{ x_center }")
print(f"y_center:{y_center}")
print('{} {} {} {}'.format(left_1, img.size[1]-top_2, left_2, img.size[1]-top_1))

# 裁剪图片便于检查,坐标原点为图片左上角
img.crop(box=(left_1, img.size[1]-top_2, left_2, img.size[1]-bottom_1)).save('crop.png')

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值