python 識別登入驗證碼圖片功能的實現程式碼(完整程式碼)
阿新 • • 發佈:2020-07-04
在編寫自動化測試用例的時候,每次登入都需要輸入驗證碼,後來想把讓python自己識別圖片裡的驗證碼,不需要自己手動登陸,所以查了一下識別功能怎麼實現,做一下筆記。
首選匯入一些用到的庫,re、Image、pytesseract、selenium、time
import re # 用於正則 from PIL import Image # 用於開啟圖片和對圖片處理 import pytesseract # 用於圖片轉文字 from selenium import webdriver # 用於開啟網站 import time # 程式碼執行停頓
首先需要獲取驗證碼圖片,才能進一步識別。
建立類,定義webdriver和find_element_by_selector方法,用來開啟網頁和定位驗證碼圖片的元素
class VerificationCode: def __init__(self): self.driver = webdriver.Firefox() self.find_element = self.driver.find_element_by_css_selector
然後開啟瀏覽器擷取驗證碼圖片
def get_pictures(self): self.driver.get('http://123.255.123.3') # 開啟登陸頁面 self.driver.save_screenshot('pictures.png') # 全屏截圖 page_snap_obj = Image.open('pictures.png') img = self.find_element('#pic') # 驗證碼元素位置 time.sleep(1) location = img.location size = img.size # 獲取驗證碼的大小引數 left = location['x'] top = location['y'] right = left + size['width'] bottom = top + size['height'] image_obj = page_snap_obj.crop((left,top,right,bottom)) # 按照驗證碼的長寬,切割驗證碼 image_obj.show() # 開啟切割後的完整驗證碼 self.driver.close() # 處理完驗證碼後關閉瀏覽器 return image_obj
未處理前的驗證碼圖片如下:
未處理的驗證碼圖片,對於python來說識別率較低,仔細看可以發現圖片裡有很對五顏六色擾亂識別的點,非常影響識別率。
下面對獲取的驗證碼進行處理。
首先用convert把圖片轉成黑白色。設定threshold閾值,超過閾值的為黑色
def processing_image(self): image_obj = self.get_pictures() # 獲取驗證碼 img = image_obj.convert("L") # 轉灰度 pixdata = img.load() w,h = img.size threshold = 160 # 該閾值不適合所有驗證碼,具體閾值請根據驗證碼情況設定 # 遍歷所有畫素,大於閾值的為黑色 for y in range(h): for x in range(w): if pixdata[x,y] < threshold: pixdata[x,y] = 0 else: pixdata[x,y] = 255 return img
經過灰度處理後的圖片
然後刪除一些擾亂識別的畫素點。
def delete_spot(self): images = self.processing_image() data = images.getdata() w,h = images.size black_point = 0 for x in range(1,w - 1): for y in range(1,h - 1): mid_pixel = data[w * y + x] # 中央畫素點畫素值 if mid_pixel < 50: # 找出上下左右四個方向畫素點畫素值 top_pixel = data[w * (y - 1) + x] left_pixel = data[w * y + (x - 1)] down_pixel = data[w * (y + 1) + x] right_pixel = data[w * y + (x + 1)] # 判斷上下左右的黑色畫素點總個數 if top_pixel < 10: black_point += 1 if left_pixel < 10: black_point += 1 if down_pixel < 10: black_point += 1 if right_pixel < 10: black_point += 1 if black_point < 1: images.putpixel((x,y),255) black_point = 0 # images.show() return images
經過去除噪點處理後的圖片
最後把處理後的圖片轉成文字。
先設定pytesseract的路徑,因為預設路徑是錯的,然後轉換圖片為文字,由於個別圖片中識別會出現處理遺漏,會被識別成空格或則點或則分號什麼的,所以增加了一個去除驗證碼中特殊字元的處理。
PS:tesseract檔案下載連結
def image_str(self): image = self.delete_spot() pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" # 設定pyteseract路徑 result = pytesseract.image_to_string(image) # 圖片轉文字 resultj = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","",result) # 去除識別出來的特殊字元 result_four = resultj[0:4] # 只獲取前4個字元 # print(resultj) # 列印識別的驗證碼 return result_four
完整程式碼如下:
import re # 用於正則 from PIL import Image # 用於開啟圖片和對圖片處理 import pytesseract # 用於圖片轉文字 from selenium import webdriver # 用於開啟網站 import time # 程式碼執行停頓 class VerificationCode: def __init__(self): self.driver = webdriver.Firefox() self.find_element = self.driver.find_element_by_css_selector def get_pictures(self): self.driver.get('http://123.255.123.3') # 開啟登陸頁面 self.driver.save_screenshot('pictures.png') # 全屏截圖 page_snap_obj = Image.open('pictures.png') img = self.find_element('#pic') # 驗證碼元素位置 time.sleep(1) location = img.location size = img.size # 獲取驗證碼的大小引數 left = location['x'] top = location['y'] right = left + size['width'] bottom = top + size['height'] image_obj = page_snap_obj.crop((left,bottom)) # 按照驗證碼的長寬,切割驗證碼 image_obj.show() # 開啟切割後的完整驗證碼 self.driver.close() # 處理完驗證碼後關閉瀏覽器 return image_obj def processing_image(self): image_obj = self.get_pictures() # 獲取驗證碼 img = image_obj.convert("L") # 轉灰度 pixdata = img.load() w,h = img.size threshold = 160 # 遍歷所有畫素,大於閾值的為黑色 for y in range(h): for x in range(w): if pixdata[x,y] = 255 return img def delete_spot(self): images = self.processing_image() data = images.getdata() w,255) black_point = 0 # images.show() return images def image_str(self): image = self.delete_spot() pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" # 設定pyteseract路徑 result = pytesseract.image_to_string(image) # 圖片轉文字 resultj = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])",result) # 去除識別出來的特殊字元 result_four = resultj[0:4] # 只獲取前4個字元 # print(resultj) # 列印識別的驗證碼 return result_four if __name__ == '__main__': a = VerificationCode() a.image_str()
看評論有很多人需要tesseract.exe檔案,但是由於檔案過大,發郵件會出現無法下載的情況,有需要的可以在一下連線裡下載tesseract.exe檔案
到此這篇關於python 識別登入驗證碼圖片(完整程式碼)的文章就介紹到這了,更多相關python識別登入驗證碼圖片內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!