前提

经常会遇到登录系统时候需要输入动态验证码的情况,但是自动化如何识别图片然后登陆系统?
在这里插入图片描述
需要用到pytesseract识别验证码图片以及PIL图像处理方法

import pytesseract
from PIL import Image, ImageEnhance

1、tesseract安装

  • OCR,即Optical Character Recognition:光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程。
  • tesseract下载地址:
https://digi.bib.uni-mannheim.de/tesseract/

选择如下进行安装
在这里插入图片描述
在安装的过程中,安装的路径一般是:
C:\Program Files (x86)\Tesseract-OCR,可以不用修改。
这里如果没有默认的话,要记住你的安装位置,因为之后需要配置系统的环境变量

配置环境变量
路径:高级系统设置——>环境变量——>系统变量中path路径——>将C:\Program Files (x86)\Tesseract-OCR添加进去。
在这里插入图片描述
配置完成后在cmd中输入tesseract -v,如果出现如下图所示,说明环境变量配置成功

E:\code\img>tesseract -V
Usage:
  tesseract --help | --help-extra | --version
  tesseract --list-langs
  tesseract imagename outputbase [options...] [configfile...]

OCR options:
  -l LANG[+LANG]        Specify language(s) used for OCR.
NOTE: These options must occur before any configfile.

Single options:
  --help                Show this help message.
  --help-extra          Show extra help for advanced users.
  --version             Show version information.
  --list-langs          List available languages for tesseract engine.

验证是否安装成功

图片准备:
在这里插入图片描述
此时,注意图片的选择,最好选择辨识度高一些的,如果直接上彩色的,会识别不成功

将该图片命名为xin.png,然后放在E盘里面的E:\code\img。然后使用cmd先到E盘的E:\code\img目录,然后使用tesseract命令进行测试。

tesseract xin.png result 

结果:

E:\code\img>tesseract xin.png result

E:\code\img>

在这里插入图片描述
多一个result文件

在这里插入图片描述

说明安装成功了

tesseract联调python

  • 安装对应模块
pip install pytesseract
  • 准备图片
将图片放入pycharm所建的工程文件夹下
  • 测试代码
from PIL import Image
import pytesseract

text = pytesseract.image_to_string(Image.open("xin.png"))
print(text)

之后运行代码,这个时候,你会遇到这个问题:

Traceback (most recent call last):
  File "D:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 170, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())
  File "D:\Python36\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "D:\Python36\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/python/20180911.py", line 4, in <module>
    text = pytesseract.image_to_string(Image.open(r'D:\chromeDownload\image.png'))
  File "D:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 294, in image_to_string
    return run_and_get_output(*args)
  File "D:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 202, in run_and_get_output
    run_tesseract(**kwargs)
  File "D:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 172, in run_tesseract
    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

处理办法

pytesseract安装后,在python的//venv//Lib目录下site-packges下会生成一个pytesseract文件夹,在文件夹中找到pytesseract.py,使用记事本之类软件打开pytesseract.py,找到如下:

tesseract_cmd = 'tesseract'

将tesseract_cmd = 'tesseract’修改为:
tesseract_cmd = ‘C:/Program Files (x86)/Tesseract-OCR/tesseract.exe’
(注意左斜杠和右斜杠,注意是自己tesseract的安装路径如果是自定义需要写对应的)
表示tesseract_cmd配置的是你安装tesseract的绝对路径,这样就能找到tesseract了。修改后保存,再去运行python代码,就可以成功了。

比如我的

# tesseract_cmd = 'tesseract'
tesseract_cmd = 'E://tool//tesseract//tesseract.exe'

在这里插入图片描述

GitHub 加速计划 / te / tesseract
60.1 K
9.29 K
下载
tesseract-ocr/tesseract: 是一个开源的光学字符识别(OCR)引擎,适用于从图像中提取和识别文本。特点是可以识别多种语言,具有较高的识别准确率,并且支持命令行和API调用。
最近提交(Master分支:1 个月前 )
bc490ea7 Don't check for a directory, because a symbolic link is also allowed. Signed-off-by: Stefan Weil <sw@weilnetz.de> 3 个月前
2991d36a - 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐