在给PC端应用做自动化测试时,某些情况下无法定位界面上的控件,但我们又想获得界面上的文字,则可以通过截图后从图片上去获取该文字信息。那么,Python中有没有对应的工具来实现OCR呢?答案是有的,它叫pytesseract。官方给它的定义如下,一起来了解和使用吧。

Python-tesseract is an optical character recognition (OCR) tool for python. That is, it will recognize and "read" the text embedded in images.

Python-tesseract is a wrapper for Google's Tesseract-OCR Engine. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types supported by the Pillow and Leptonica imaging libraries, including jpeg, png, gif, bmp, tiff, and others. Additionally, if used as a script, Python-tesseract will print the recognized text instead of writing it to a file.

安装

1.首先下载并安装teseseract安装包,下载地址:https://digi.bib.uni-mannheim.de/tesseract/

2.安装完成后,添加系统环境变量。

3.安装对应的Python库。在实践过程中,单独安装pytesseract时会报错,需要与pillow一起安装。

pip install pillow
pip install pytesseract

4.根据需要解析的文字语言,下载对应的语言包,下载地址:https://github.com/tesseract-ocr/tessdata ,拿中文语言包举例,下载chi_sim.traineddata后,将其放入Teseseract-OCR安装目录下的tessdata目录即可。

使用

举个例子,想要提取图片中的“酌三巡”三个字。

使用方法非常简单,调用pytesseract.image_to_string()方法即可。

from PIL import Image
import pytesseract img = Image.open("demo.png")
ocr_text = pytesseract.image_to_string(img, lang="chi_sim")
print("提取结果为:", ocr_text)

运行结果:

参考资料

  • https://github.com/madmaze/pytesseract
  • https://github.com/tesseract-ocr/tesseract

最新文章

  1. 在 Linux 上配置一个 syslog 服务器
  2. Linux makefile 教程 非常详细,且易懂 (转)
  3. Android退出程序时的"再按一次退出"实现
  4. MyBatis动态SQL语法
  5. OpenRisc-42-or1200的ALU模块分析
  6. ios 调节器 modal 得知
  7. OpenCV成长之路:直线、轮廓的提取与描述
  8. 使用FusionCharts出柱状图和饼状图
  9. Android开发环境的配置2017.05.27
  10. 关于null的判断
  11. Android 9.png图片的制作方法
  12. 从零开始学 Web 之 DOM(七)事件冒泡
  13. 学习Spring Boot:(二十二)使用 AOP
  14. AbstractBootstrap.bind()
  15. Netty源码分析之NioEventLoop(二)—NioEventLoop的启动
  16. IP冲突解决方案
  17. 三维凸包求凸包表面的个数(HDU3662)
  18. 【文文殿下】快速傅里叶变换(FFT)学习笔记
  19. nginx 根据get参数重定向(根据电视访问的mac地址传递的值,来重定向访问别的url地址,这样就可以进行单台的测试环境。。)
  20. 三分 - HNU 13409 Flowers

热门文章

  1. argparse模块基本用法
  2. synchronized 优化手段之锁膨胀机制!
  3. CFS靶机
  4. Synology群晖100TB万兆文件云服务器NAS存储池类别 RAID 6 (有数据保护)2021年7月29日 - Copy
  5. Linux下-LNMP环境搭建博客网站(全过程)
  6. docker容器网络bridge
  7. Mol Cell | 张令强/贺福初/魏文毅/刘翠华揭示线性泛素化调控血管生成新机制
  8. 跟我一起写 Makefile(九)
  9. Golang语言系列-11-goroutine并发
  10. Java Lambda 表达式源码分析