温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python中怎么批量识别图片指定区域的文字

发布时间:2021-06-16 17:48:16 来源:亿速云 阅读:916 作者:Leah 栏目:开发技术

python中怎么批量识别图片指定区域的文字,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1.aircv

用于识别模板再原始图的位置坐标

pip install aircv

2.Pillow

用于剪裁图片

pip install Pillow

3.Tesseract

文字识别
在此也可以用平台端的API进行更精准的识别
ubuntu下Tesseract环境安装

sudo apt-get install libpng12-dev 
sudo apt-get install libjpeg62-dev 
sudo apt-get install libtiff4-dev 
sudo apt-get install gcc 
sudo apt-get install g++ 
sudo apt-get install automake

1.tesseract-ocr安装

sudo apt-get install tesseract-ocr

2.pytesseract安装

pip install pytesseract

Python代码

识别对应位置

#!/usr/bin/python2.7 
# -*- coding: utf-8 -*- 
import aircv


def matchImg(imgsrc, imgobj, confidence=0.2):
 """
  图片对比识别imgobj在imgsrc上的相对位置(批量识别统一图片中需要的部分)
 :param imgsrc: 原始图片路径(str)
 :param imgobj: 待查找图片路径(模板)(str)
 :param confidence: 识别度(0<confidence<1.0)
 :return: None or dict({'confidence': 相似度(float), 'rectangle': 原始图片上的矩形坐标(tuple), 'result': 中心坐标(tuple)})
 """
 imsrc = aircv.imread(imgsrc)
 imobj = aircv.imread(imgobj)

 match_result = aircv.find_template(imsrc, imobj,
         confidence) # {'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)}
 if match_result is not None:
  match_result['shape'] = (imsrc.shape[1], imsrc.shape[0]) # 0为高,1为宽

 return match_result

图片剪裁

#!/usr/bin/python2.7 
# -*- coding: utf-8 -*- 
from PIL import Image, ImageEnhance

def cutImg(imgsrc, out_img_name, coordinate):
 """
  根据坐标位置剪切图片
 :param imgsrc: 原始图片路径(str)
 :param out_img_name: 剪切输出图片路径(str)
 :param coordinate: 原始图片上的坐标(tuple) egg:(x, y, w, h) ---> x,y为矩形左上角坐标, w,h为右下角坐标
 :return:
 """
 image = Image.open(imgsrc)
 region = image.crop(coordinate)
 region = ImageEnhance.Contrast(region).enhance(1.5)
 region.save(out_img_name)

图片识别

#!/usr/bin/python2.7 
# -*- coding: utf-8 -*- 
import pytesseract
from PIL import Image

image = Image.open('bb.png')
code = pytesseract.image_to_string(image)
print(code)

关于python中怎么批量识别图片指定区域的文字问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI