pytesseract的使用 | python识别验证码

1. 安装tesseract

详见:
https://blog.csdn.net/lijiamingccc/article/details/119459775

2.安装pytesseract

在pycharm终端下,安装 pytesseract,如图所示

pip install pytesseract

在这里插入图片描述

3. 修改包中部分代码

在这里插入图片描述
文件中的这个路径,改成第一步你安装的位置,建议找到之后直接复制
前面加个r,是为了说明这个路径是一段字符串,防止转义

4.代码网站测试

代码:

网址可以自己随便找一个

    res = requests.get(url="x'x'x'x'x")

    with open('image.jpg', 'wb') as fw:
        fw.write(res.content)

    img = cv2.imread("image.jpg")

    # 四周置白色  图片降噪
    def around_white(img):
        w, h, s = img.shape
        for _w in range(w):
            for _h in range(h):
                if (_w <= 5) or (_h <= 5) or (_w >= w-5) or (_h >= h-5):
                    img.itemset((_w, _h, 0), 255)
                    img.itemset((_w, _h, 1), 255)
                    img.itemset((_w, _h, 2), 255)
        return img
	
    img2 = around_white(img)
    ret, img2 = cv2.threshold(img2, 150, 255, cv2.THRESH_BINARY)
	
	 # 前面需要添加一些图像降噪的操作
    code = pytesseract.image_to_string(img2,config="--psm 6 digits")
    code = re.findall('\d+',code)[0]
    print("识别的验证码为:{}".format(code))

效果:
在这里插入图片描述
识别效果不一定保证每次都正确,但是基本上5次之内都是可以成功的,这里可以写一个循环,直到成功为止。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码魔法师!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值