資源簡介
使用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?????????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個文件信息
評論
共有 條評論