本文以Windows为例
一、下载安装tesseract-ocr
1.github地址: https://github.com/tesseract-ocr/tesseract
tesseract-ocr-setup-3.05.01.exe下载地址:http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.01.exe
tesseract-ocr-setup-4.00.00dev.exe下载地址:http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe
以上二个版本任选其中一个下载即可
如需中文字体可下载后放到tesseract-ocr安装后的tessdata目录
简体字识别包:https://raw.githubusercontent.com/tesseract-ocr/tessdata/4.00/chi_sim.traineddata
繁体字识别包:https://github.com/tesseract-ocr/tessdata/raw/4.0/chi_tra.traineddata
2.安装过程中选择安装目录
3.配置环境变量
在环境变量的系统变量PATH,把tesseract-ocr的安装路径(如 E:\Tesseract-OCR;)添加进去。
在环境变量的系统变量中,增加一个TESSDATA_PREFIX变量名,变量值还是tesseract-ocr的安装路径(如E:\Tesseract-OCR; )
4.验证是否安装成功
cmd窗口,敲入tesseract,应该如下信息则表示安装成功。
还可以用命令tesseract --list-langs来查看Tesseract-OCR支持语言。
二、安装Pillow
pip install Pillow或pythonsetup.py install (源码安装)
三、安装pytesseract。
pip install pytesseract或pythonsetup.py install (源码安装)
四、Python调用
通过调用接口返回的二维码图片
# coding:utf-8
# @author : csl
# @date : 2018/11/28 17:33
import requests
import os
import pytesseract
from PIL import Image
class imageCode2str(object):
"""图片二维码转换为str"""
def __init__(self, url):
self.url = url
def saveImage(self,):
"""
@description: 保存图片
:param url:
:return:
"""
# 获取文件路径
PATH = os.path.abspath(os.path.join(os.path.dirname('.'), os.path.pardir))
self.imagePATH = str(PATH) + '\\***.png' # 拼接日志目录
if "***" not in self.imagePATH:
self.imagePATH = str(PATH) + "\\***.png"
# 保存图片
reqimage = requests.get(self.url)
f_image = open(self.imagePATH, "wb")
f_image.write(reqimage.content)
f_image.close()
def image2str(self):
"""图片二维码提取"""
self.saveImage()
image = Image.open(self.imagePATH)
vcode = pytesseract.image_to_string(image)
print(vcode)
if __name__ == "__main__":
imageCode2str("http://******?cid=ADMIN_LOGIN").image2str()
如已有图片则只需
import os import pytesseract from PIL import Image
image = Image.open("***.png") vcode = pytesseract.image_to_string(image) print(vcode)
感谢:https://blog.csdn.net/duanshao/article/details/79514051