python 验证码识别改进版

本文介绍了一种通过Python实现的验证码识别方法。主要步骤包括加载图片、灰度转换、二值化处理及图片降噪等,最后利用OCR技术进行字符识别。通过对图片进行预处理,显著提高了识别准确率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


识别上图所示的验证码

#!/usr/bin/python
# encoding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import requests
from lxml import etree
import time,datetime
import re
from switch import *
from PIL import Image
import sys
import pyocr
import pyocr.builders
import pytesseract

image_path = 'XXX.png'

im = Image.open(image_path)
imgry = im.convert('L')
threshold = 140
table = []
for i in range(256):
  if i < threshold:
      table.append(0)
  else:
      table.append(1)
out = imgry.point(table, '1')
out.show()
#读取出字符串
cap_str = pytesseract.image_to_string(out)
print cap_str



out.show()   是图片降噪处理后的效果


然后使用  image_to_string  识别  准确率就提高很多了、
图片降噪后的处理结果
<img src="https://img-blog.youkuaiyun.com/20160604175525790?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值