python爬虫验证码识别模块tesseracr与pytesseract

22 篇文章 2 订阅
1 篇文章 0 订阅

由于tesserocr在windows环境下会出现各种不兼容问题,并且与pycharm虚拟环境不兼容等问题,所以在windows系统环境下,选择pytesseract模块进行安装,如果实在要安装请使用whl文件安装或者使用conda安装

pip install pytesseract

如果在pytesseract运行是找不到tesseract解释器,这种情况一般是在虚拟环境下会发生,我们需要将tesseract-OCR的执行文件tesseract.ext配置到windows系统中的PATH环境中,或者修改pytesseract.py文件,将其中的“tesseract_cmd”字段指定为tesseract.exe的完整路径即可

测试识别功能:

import pytesseract
from PIL import Image

image = Image.open('tesseracttest.png')		# 图片名
text = pytesseract.image_to_string(image)
print(text)

Ubuntu,linux系统中,安装命令如下

#安装tesseract
sudo apt-get install -y tesseract-ocr libtesseract-dev libleptonica-dev

#安装语言包
git clone https://github.com/tesseract-ocr/tessdata.git
sudo mv tessdata/* /usr/share/tesseract-ocr/tessdata

#安装pytesseract
pip3 install pytesseract

识别图片里的内容再写入另外一张图片里

from PIL import Image
import subprocess

def cleanFile(filePath, newFilePath):
    image = Image.open(filePath)

    # 对图片进行阈值过滤(低于143的置为黑色,否则为白色)
    image = image.point(lambda x: 0 if x < 143 else 255)
    # 重新保存图片
    image.save(newFilePath)

    # 调用系统的tesseract命令对图片进行OCR识别
    subprocess.call(["tesseract", newFilePath, "output"])

    # 打开文件读取结果
    with open("output.txt", 'r') as f:
        print(f.read())

if __name__ == "__main__":
    cleanFile("tesseracttest.jpg", "123.jpg")    # 读取tesseracttest内的文字,再把文字写入123中

这次简单识别下次再更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值