python验证码简单识别
因为需求,所以接触了验证码这一块,原本感觉到会很难,学了之后挺简单的,但后来又发现自己还是too young。。。
PIL(python Image Library)
目前PIL的官方最新版本为1.1.7,支持的版本为python 2.5, 2.6, 2.7,
PIL官方网站:http://www.pythonware.com/products/pil/
不支持python3,但有高手把它重新编译生成python3下可安装的exe了。这一非官方下载地址 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil ,
,它已经多年未更新了,差不多已经被遗弃了。
pillow
Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库。pillow可以说已经取代了PIL,将其封装成python的库(pip即可安装),且支持python2和python3,目前最新版本是3.0.0。
Pillow的Github主页:https://github.com/python-pillow/Pillow
Pillow的文档(对应版本v3.0.0):
https://pillow.readthedocs.org/en/latest/handbook/index.html
安装它很简单 pip install pillow
使用方式:
#python2
import Image
#python3(因为是派生的PIL库,所以要导入PIL中的Image)from PIL import Image
以python3为例,
- open
from PIL import Image
im = Image.open("1.png")
im.show()
- format
format属性定义了图像的格式,如果图像不是从文件打开的,那么该属性值为None;size属性是一个tuple,表示图像的宽和高(单位为像素);mode属性为表示图像的模式,常用的模式为:L为灰度图,RGB为真彩色,CMYK为pre-press图像。如果文件不能打开,则抛出IOError异常。
print(im.format, im.size, im.mode)
- save
im.save("c:\\")
- convert()
convert() 是图像实例对象的一个方法,接受一个 mode 参数,用以指定一种色彩模式,mode 的取值可以是如下几种:
· 1 (1-bit pixels, black and white, stored with one pixel per byte)
· L (8-bit pixels, black and white)
· P (8-bit pixels, mapped to any other mode using a colour palette)
· RGB (3x8-bit pixels, true colour)
· RGBA (4x8-bit pixels, true colour with transparency mask)
· CMYK (4x8-bit pixels, colour separation)
· YCbCr (3x8-bit pixels, colour video format)
· I (32-bit signed integer pixels)
· F (32-bit floating point pixels)
im = Image.open('1.png').convert('L')
- Filter
from PIL import Image, ImageFilter
im = Image.open(‘1.png’)
# 高斯模糊
im.filter(ImageFilter.GaussianBlur)
# 普通模糊
im.filter(ImageFilter.BLUR)
# 边缘增强
im.filter(ImageFilter.EDGE_ENHANCE)
# 找到边缘
im.filter(ImageFilter.FIND_EDGES)
# 浮雕
im.filter(ImageFilter.EMBOSS)
# 轮廓
im.filter(ImageFilter.CONTOUR)
# 锐化
im.filter(ImageFilter.SHARPEN)
# 平滑
im.filter(ImageFilter.SMOOTH)
# 细节
im.filter(ImageFilter.DETAIL)
查看图像直方图
im.histogram()转换图像文件格式
def img2jpg(imgFile):
if type(imgFile)==str and imgFile.endswith(('.bmp', '.gif', '.png')):
with Image.open(imgFile) as im:
im.convert('RGB').save(imgFile[:-3]+'jpg')
img2jpg('1.gif')
img2jpg('1.bmp')
img2jpg('1.png')
屏幕截图
from PIL import ImageGrab
im = ImageGrab.grab((0,0,800,200)) #截取屏幕指定区域的图像
im = ImageGrab.grab() #不带参数表示全屏幕截图图像裁剪与粘贴
box = (120, 194, 220, 294) #定义裁剪区域
region = im.crop(box) #裁剪
region = region.transpose(Image.ROTATE_180)
im.paste(region,box) #粘贴图像缩放
im = im.resize((100,100)) #参数表示图像的新尺寸,分别表示宽度和高度
图像对比度增强
from PIL import Image
from PIL import ImageEnhance
#原始图像
image = Image.open('lena.jpg')
image.show()
#亮度增强
enh_bri = ImageEnhance.Brightness(image)
brightness = 1.5
image_brightened = enh_bri.enhance(brightness)
image_brightened.show()
#色度增强
enh_col = ImageEnhance.Color(image)
color = 1.5
image_colored = enh_col.enhance(color)
image_colored.show()
#对比度增强
enh_con = ImageEnhance.Contrast(image)
contrast = 1.5
image_contrasted = enh_con.enhance(contrast)
image_contrasted.show()
#锐度增强
enh_sha = ImageEnhance.Sharpness(image)
sharpness = 3.0
image_sharped = enh_sha.enhance(sharpness)
image_sharped.show()
pytesseract
Python-tesseract是一款用于光学字符识别(OCR)的python工具,即从图片中识别出其中嵌入的文字。Python-tesseract是对Google Tesseract-OCR的一层封装。它也同时可以单独作为对tesseract引擎的调用脚本,支持使用PIL库(Python Imaging Library)读取的各种图片文件类型,包括jpeg、png、gif、bmp、tiff和其他格式,。作为脚本使用它将打印出识别出的文字而非写入到文件。所以安装pytesseract前要先安装PIL(也就是现在的pillow)和tesseract-orc这俩依赖库
tesseract-ocr是谷歌的开源项目,用于图像文本识别,具体安装使用情况可以参照图片文字OCR识别-tesseract-ocr4.00.00安装使用(4.000版本是实验版,建议使用正式版的3.X)
pytesseract是调用tesseract的一个库,所以必须先安装好tesseract,
如何安装pytesseract
pip install pytesseract
如何使用pytesseract
from PIL import Image#后面的lang是语言的意思,fra是法语的意思.
import pytesseract
print(pytesseract.image_to_string(Image.open('test.png')))
print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))
很简单,pytesseract只有一个简单的image_to_string方法。
示例
图片:
from PIL import Image
from PIL import ImageEnhance
import pytesseract
im=Image.open("1.jpg")
im=im.convert('L')
im.show()
im=ImageEnhance.Contrast(im)
im=im.enhance(3)
im.show()
print(pytesseract.image_to_string(im))
结果:
图一:
图二:
打印结果:
打码平台
众所周知,上面的验证码识别都是小儿科,也就简单的上面可以去去噪,换换灰度什么的,要是碰到了神奇的干扰线,或者奇葩的字母组合,那就基本上歇菜了,目前的验证码识别依旧存在着巨大的问题,所以打码平台也就经久不衰了,这么多年过去了,非但没有就此陌去,反而得到了飞速的发展,其中几个比较有名,下次有时间再来补充。。。
——来补充了———
这次选择的是云打码平台(非广告),当然啦,只是本人选择的是云打码,事实上其他的打码平台都是类似的,
首先要分清楚云打码平台的账户种类
云打码平台分两种:用户和开发者
我们只需使用用户就可以了
注册完之后可以选择充值,先充1块钱,有2000积分,下面是使用积分的说明:
也就是说1块钱大概可以使用50次以上,这还是可以接受的
接下来就简单多了
用户username: ……..
密码password: ………
账户余额balance:………..分
只需修改验证码地址和验证码种类,填好注册时的用户名和密码,然后运行即可输出相关信息,目测新浪微博验证码识别率95%以上
直接运行下面的源码即可(记得改好上面的要求)
import http.client, mimetypes, urllib, json, time, requests
######################################################################
#验证码地址**(记得修改)**
Image="C:\\Users\\Desktop\\ORC\\1.png"
#验证码种类**(记得修改)**
Species=1005
class YDMHttp:
apiurl = 'http://api.yundama.com/api.php'
username = ''
password = ''
appid = ''
appkey = ''
def __init__(self, username, password, appid, appkey):
self.username = username
self.password = password
self.appid = str(appid)
self.appkey = appkey
def request(self, fields, files=[]):
response = self.post_url(self.apiurl, fields, files)
response = json.loads(response)
return response
def balance(self):
data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
response = self.request(data)
if (response):
if (response['ret'] and response['ret'] < 0):
return response['ret']
else:
return response['balance']
else:
return -9001
def login(self):
data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
response = self.request(data)
if (response):
if (response['ret'] and response['ret'] < 0):
return response['ret']
else:
return response['uid']
else:
return -9001
def upload(self, filename, codetype, timeout):
data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
file = {'file': filename}
response = self.request(data, file)
if (response):
if (response['ret'] and response['ret'] < 0):
return response['ret']
else:
return response['cid']
else:
return -9001
def result(self, cid):
data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)}
response = self.request(data)
return response and response['text'] or ''
def decode(self, filename, codetype, timeout):
cid = self.upload(filename, codetype, timeout)
if (cid > 0):
for i in range(0, timeout):
result = self.result(cid)
if (result != ''):
return cid, result
else:
time.sleep(1)
return -3003, ''
else:
return cid, ''
def post_url(self, url, fields, files=[]):
for key in files:
files[key] = open(files[key], 'rb');
res = requests.post(url, files=files, data=fields)
return res.text
######################################################################
# 用户名(填自己的)**(记得修改)**
username = '*************'
# 密码(填自己的)**(记得修改)**
password = '*******'
# 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!(非开发者不用管)
appid = 1
# 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!(非开发者不用管)
appkey = '22cc5376925e9387a23cf797cb9ba745'
# 图片文件
filename = Image
# 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
codetype = Species
# 超时时间,秒
timeout = 60
# 检查
if (username == 'username'):
print('请设置好相关参数再测试')
else:
# 初始化
yundama = YDMHttp(username, password, appid, appkey)
# 登陆云打码
uid = yundama.login();
print('uid: %s' % uid)
# 查询余额
balance = yundama.balance();
print('balance: %s' % balance)
# 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
cid, result = yundama.decode(filename, codetype, timeout);
print('cid: %s, result: %s' % (cid, result))
######################################################################
附赠几张新浪微博的验证码:
小结
验证码是一个神奇的产物,它充分的遏制住了一大批计算机上网的冲动,就是这么一个小小的验证码阻挡住了千军万马,但是我相信未来的算法一定会将这个产物解决掉的-。-。-。-。-。-。-。
python验证码简单识别的更多相关文章
- 基于Python使用SVM识别简单的字符验证码的完整代码开源分享
关键字:Python,SVM,字符验证码,机器学习,验证码识别 1 概述 基于Python使用SVM识别简单的验证字符串的完整代码开源分享. 因为目前有了更厉害的新技术来解决这类问题了,但是本文作 ...
- python 验证码识别示例(五) 简单验证码识别
今天介绍一个简单验证的识别. 主要是标准的格式,没有扭曲和变现.就用 pytesseract 去识别一下. 验证码地址:http://wscx.gjxfj.gov.cn/zfp/webroot/xfs ...
- python实现对简单的运算型验证码的识别【不使用OpenCV】
最近在写我们学校的教务系统的手机版,在前端用户执行绑定操作后,服务器将执行登录,但在登录过程中,教务系统中有个运算型的验证码,大致是这个样子的: 下面我们开始实现这个验证码的识别. 1.图片读取 从网 ...
- python验证码识别
关于利用python进行验证码识别的一些想法 用python加“验证码”为关键词在baidu里搜一下,可以找到很多关于验证码识别的文章.我大体看了一下,主要方法有几类:一类是通过对图片进行处 理,然后 ...
- python 验证码识别示例(一) 某个网站验证码识别
某个招聘网站的验证码识别,过程如下 一: 原始验证码: 二: 首先对验证码进行分析,该验证码的数字颜色有变化,这个就是识别这个验证码遇到的比较难的问题,解决方法是使用PIL 中的 getpixel ...
- 【转】Python验证码识别处理实例
原文出处: 林炳文(@林炳文Evankaka) 一.准备工作与代码实例 1.PIL.pytesser.tesseract (1)安装PIL:下载地址:http://www.pythonware.com ...
- Python 验证码识别(别干坏事哦...)
关于python验证码识别库,网上主要介绍的为pytesser及pytesseract,其实pytesser的安装有一点点麻烦,所以这里我不考虑,直接使用后一种库. python验证码识别库安装 要安 ...
- Python爬虫学习笔记之微信宫格验证码的识别(存在问题)
本节我们将介绍新浪微博宫格验证码的识别.微博宫格验证码是一种新型交互式验证码,每个宫格之间会有一条 指示连线,指示了应该的滑动轨迹.我们要按照滑动轨迹依次从起始宫格滑动到终止宫格,才可以完成验证,如 ...
- 【python】入门级识别验证码
前情:这篇文章所提及的内容是博主上个暑假时候做的,一直没有沉下心来把自己的心得写在纸面上,所幸这个假期闲暇时候比较多,想着能写多少是多少,于是就有了此篇. 验证码?我也能破解? 关于验证码的介绍就不多 ...
随机推荐
- Java中的Unsafe类111
1.Unsafe类介绍 Unsafe类是在sun.misc包下,不属于Java标准.但是很多Java的基础类库,包括一些被广泛使用的高性能开发库都是基于Unsafe类开发的,比如Netty.Hadoo ...
- 在当前图纸中创建一个表格, AcDbTable 类
Table 例子学习笔记在这个例子中,ARX向我们展示了ACDBTABLE类的一些基本操作方法,ACDBTABLE类是ACAD2005及其以后的产品,应该是说ACDBDATATABLE的升级产品,Ac ...
- MyBatis新手教程(一)
MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apache 迁移到了 google,并改名为MyBatis,2013年迁移到Github. MyBatis是一个优秀的持 ...
- Nginx+Keepalived 集群方案
1.Keepalived高可用软件 Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,kee ...
- #Java学习之路——基础阶段二(第九篇)
我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...
- 『OGG 02』Win7 配置 Oracle GoldenGate Adapter Java 踩坑指南
上一文章 <__Win7 配置OGG(Oracle GoldenGate).docx>定下了 两个目标: 目标1: 给安装的Oracle_11g 创建 两个用户 admin 和 root ...
- MyISAM加锁分析
为什么加锁 你正在读着你喜欢的女孩递给你的信,看到一半的时候,她的好闺蜜过来瞄了一眼(假设她会隐身术,你看不到她),她想把"我很喜欢你"改成"我不喜欢你",刚把 ...
- .netcore2.1 使用postgresql数据库,不能实现表的CRUD问题
PostgreSQL对表名.字段名都是区分大小写的.为了兼容其他的数据库程序代码的编写,推荐使用小写加_的方式,例如:swagger_info 我们使用.netcore连接postgresql数据库时 ...
- 搜狗输入法与VS快捷键有冲突_处理办法
前言:搜狗输入法是大家常用的文字输入工具,但是在开启输入法的时候,VS的一些快捷键无法正常使用,如智能提示快捷键:Ctrl+.,这就非常尴尬了,除非把输入法切换成英文或者卸载搜狗改别的输入法,一个是切 ...
- Restframe_work 回顾记忆集
目录 Restframe_work 回顾记忆集 rest_framework主要功能介绍 rest_framework主要模块介绍 记忆集 错题集 混淆集 重点集 难点集 Restframe_work ...