资源简介
使用python+robot framework识别图片验证码
前提:安装PIL

代码片段和文件信息
#!/usr/bin/python
# -*-coding:utf-8-*-
from PIL import Image ImageDraw
def cut_pinnum_pic_from_page(xpath window pageScreen pinNumPath):
‘‘‘
url=‘http://10.2.122.143:8000/?HIPPO_TRADE_URL=ws://10.2.122.143:1521#StockQuote‘
driver = webdriver.Chrome()
driver.maximize_window() #将浏览器最大化
driver.get(url)
driver.save_screenshot(‘C:\\aa.png‘) #截取当前网页,该网页有我们需要的验证码
‘‘‘
driver = window
#pageScreen = “C:\\yzm\\page.png“
#pinNumPath = ‘C:\\yzm\\pinnum.jpg‘
driver.save_screenshot(pageScreen)
imgelement = driver.find_element_by_xpath(xpath)
#获取验证码xy轴坐标
location = imgelement.location
size=imgelement.size
#写成我们需要截取的位置坐标
rangle=(int(location[‘x‘]) int(location[‘y‘]) int(location[‘x‘] + size[‘width‘]) int(location[‘y‘] + size[‘height‘]))
i=Image.open(pageScreen)
#使用Image的crop函数,截取验证码
pin=i.crop(rangle)
pin.save(pinNumPath)
print “:::“ pinNumPath “saved successfully!“
def getPixel(image x y G N):
‘‘‘
#二值判断如果确认是噪声用改点的上面一个点的灰度进行替换
#该函数也可以改成RGB判断的具体看需求如何
‘‘‘
L = image.getpixel((x y))
if L > G:
L = True
else:
L = False
nearDots = 0
if L == (image.getpixel((x - 1 y - 1)) > G):
nearDots += 1
if L == (image.getpixel((x - 1 y)) > G):
nearDots += 1
if L == (image.getpixel((x - 1 y + 1)) > G):
nearDots += 1
if L == (image.getpixel((x y - 1)) > G):
nearDots += 1
if L == (image.getpixel((x y + 1)) > G):
nearDots += 1
if L == (image.getpixel((x + 1 y - 1)) > G):
nearDots += 1
if L == (image.getpixel((x + 1 y)) > G):
nearDots += 1
if L == (image.getpixel((x + 1 y + 1)) > G):
nearDots += 1
if nearDots < N:
return image.getpixel((x y-1))
else:
return None
def clearNoise(image G N Z):
‘‘‘
降噪
根据一个点A的RGB值,与周围的8个点的RBG值比较,设定一个值N(0 G: Integer 图像二值化阀值
N: Integer 降噪率 0 Z: Integer 降噪次数
@输出
0:降噪成功
1:降噪失败
‘‘‘
draw = ImageDraw.Draw(image)
for i in xrange(0 Z):
for x in xrange(1 image.size[0] - 1):
for y in xrange(1 image.size[1] - 1):
color = getPixel(image x y G N)
if color != None:
draw.point((x y) color)
def op_clearNoise(oldPic newPic):
‘‘‘
执行去噪处理
‘‘‘
image = Image.open(oldPic)
image = image.convert(“L“)
clearNoise(image 50 4 4)
print “:::clearNoise successfully!“
image.save(newPic)
if __name__==‘__main__‘:
#oldPic = ‘C:\\yzm\\pinnum.jpg‘
#newPic = ‘C:\\yzm\\pin_clearnoise.jpg‘
oldPinPic = ‘C:\\yzm\\pinnum.jpg‘
newPinPic = ‘C:\\yzm\\pin_clearnoise.jpg‘
op_clearNoise(oldPinPic newPinPic)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2095 2016-06-16 10:05 pytesser\123.png
文件 273 2016-06-16 10:05 pytesser\AUTHORS
文件 48 2016-06-16 10:05 pytesser\ChangeLog
文件 3289 2016-06-16 18:13 pytesser\clearNoise.py
文件 1594 2016-06-16 10:05 pytesser\delPic.py
文件 424 2016-06-16 10:05 pytesser\errors.py
文件 1410 2016-06-16 10:05 pytesser\fnord.tif
文件 20607 2016-06-16 10:05 pytesser\fonts_test.png
文件 558 2016-06-16 10:05 pytesser\LICENSE
文件 337 2016-06-16 10:05 pytesser\NOTICE
文件 38668 2016-06-16 10:05 pytesser\phototest.tif
文件 3987 2016-06-16 18:13 pytesser\pytesser.py
文件 2652 2016-06-16 10:05 pytesser\README
文件 2772 2016-06-16 10:05 pytesser\tessdata\blackText.params
文件 1012 2016-06-16 10:05 pytesser\tessdata\configs\api_config
文件 760 2016-06-16 10:05 pytesser\tessdata\configs\api_resaljet
文件 412 2016-06-16 10:05 pytesser\tessdata\configs\box.train
文件 97 2016-06-16 10:05 pytesser\tessdata\configs\inter
文件 815 2016-06-16 10:05 pytesser\tessdata\configs\oldapi_config
文件 816 2016-06-16 10:05 pytesser\tessdata\configs\oldbox.train
文件 1785 2016-06-16 10:05 pytesser\tessdata\configs\variable_config
文件 1068 2016-06-16 10:05 pytesser\tessdata\configs\var_api_config
文件 1071 2016-06-16 10:05 pytesser\tessdata\configs\var_box.train
文件 12 2016-06-16 10:05 pytesser\tessdata\confsets
文件 235 2016-06-16 10:05 pytesser\tessdata\DangAmbigs
文件 132988 2016-06-16 10:05 pytesser\tessdata\fmtable.cls
文件 751 2016-06-16 10:05 pytesser\tessdata\fnetwts
文件 720 2016-06-16 10:05 pytesser\tessdata\freq-dawg
文件 676716 2016-06-16 10:05 pytesser\tessdata\inttemp
文件 1369167 2016-06-16 10:05 pytesser\tessdata\netwts
............此处省略21个文件信息
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论