(源码分享)利用Python识别提取图像文字(中文英文都可以)
你想了解怎么利用程序自动识别网站验证码吗?识别提取图像文字(中文英文都可以)
分享一点简单有用的小项目:python
源码分享如下:
看视频教程链接:(点击识别图像文字视频教程链接)
一、首先需要安装Tesseract模块及 语言包
Tesseract OCR光学字符识别
Windows系统:
安装网站 (放在不需要权限的纯英文路径下):
: https://digi.bib.uni-mannheim.de/tesseract/
可以下载一些语言包:
https://github.com/tesseract-ocr/ 第二步:设置环境变量:
安装完成后,如果想要在命令行中使用Tesseract,那么应该设置环境变量。
还有一个环境变量需要设置的是,要把训练的数据文件路径也放到环境变量中。
在环境变量中,添加一个TESSDATA_PREFIX=C:path_to_tesseractdata eseractdata。 第三步:在代码中使用tesseract识别图像:
在Python代码中操作tesseract。需要安装一个库,叫做pytesseract。通过pip的方式即可安装:
pip install pytesseract
并且,需要读取图片,需要借助一个第三方库叫做PIL。通过pip list看下是否安装。如果没有安装,通过pip的方式安装:
pip install PIL
使用pytesseract将图片上的文字转换为文本文字的示例代码如下: # 导入pytesseract库 import pytesseract # 导入Image库 from PIL import Image # 指定tesseract.exe所在的路径 pytesseract.pytesseract.tesseract_cmd = r"D:ProgramAppTesseractOCR esseract.exe" # 打开图片 image = Image.open("a.png") # 调用image_to_string将图片转换为文字 text = pytesseract.image_to_string(image) #text = pytesseract.image_to_string(image,lang="chi_sim") print(text)