最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python識別驗證碼圖片實例詳解

 更新時間:2020年02月17日 16:08:49   作者:沉默的鵬先生  
這篇文章主要介紹了python識別驗證碼圖片實例詳解,需要的朋友可以參考下

在編寫自動化測試用例的時候,每次登錄都需要輸入驗證碼,后來想把讓python自己識別圖片里的驗證碼,不需要自己手動登陸,所以查了一下識別功能怎么實現(xiàn),做一下筆記。

首選導(dǎo)入一些用到的庫,re、Image、pytesseract、selenium、time

import re # 用于正則
from PIL import Image # 用于打開圖片和對圖片處理
import pytesseract # 用于圖片轉(zhuǎn)文字
from selenium import webdriver # 用于打開網(wǎng)站
import time # 代碼運行停頓

首先需要獲取驗證碼圖片,才能進(jìn)一步識別。

創(chuàng)建類,定義webdriver和find_element_by_selector方法,用來打開網(wǎng)頁和定位驗證碼圖片的元素

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 # 獲取驗證碼的大小參數(shù)
  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() # 處理完驗證碼后關(guān)閉瀏覽器
  return image_obj

未處理前的驗證碼圖片如下:

未處理的驗證碼圖片,對于python來說識別率較低,仔細(xì)看可以發(fā)現(xiàn)圖片里有很對五顏六色擾亂識別的點,非常影響識別率。

下面對獲取的驗證碼進(jìn)行處理。

首先用convert把圖片轉(zhuǎn)成黑白色。設(shè)置threshold閾值,超過閾值的為黑色

 def processing_image(self):
  image_obj = self.get_pictures() # 獲取驗證碼
  img = image_obj.convert("L") # 轉(zhuǎn)灰度
  pixdata = img.load()
  w, h = img.size
  threshold = 160 # 該閾值不適合所有驗證碼,具體閾值請根據(jù)驗證碼情況設(shè)置
  # 遍歷所有像素,大于閾值的為黑色
  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

經(jīng)過灰度處理后的圖片

然后刪除一些擾亂識別的像素點。

 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)]
     # 判斷上下左右的黑色像素點總個數(shù)
     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

經(jīng)過去除噪點處理后的圖片

最后把處理后的圖片轉(zhuǎn)成文字。

先設(shè)置pytesseract的路徑,因為默認(rèn)路徑是錯的,然后轉(zhuǎn)換圖片為文字,由于個別圖片中識別會出現(xiàn)處理遺漏,會被識別成空格或則點或則分號什么的,所以增加了一個去除驗證碼中特殊字符的處理。

 def image_str(self):
  image = self.delete_spot()
  pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" # 設(shè)置pyteseract路徑
  result = pytesseract.image_to_string(image) # 圖片轉(zhuǎn)文字
  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 # 用于圖片轉(zhuǎn)文字
from selenium import webdriver # 用于打開網(wǎng)站
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 # 獲取驗證碼的大小參數(shù)
  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() # 處理完驗證碼后關(guān)閉瀏覽器
  return image_obj
 
 def processing_image(self):
  image_obj = self.get_pictures() # 獲取驗證碼
  img = image_obj.convert("L") # 轉(zhuǎn)灰度
  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)]
     # 判斷上下左右的黑色像素點總個數(shù)
     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
 
 def image_str(self):
  image = self.delete_spot()
  pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" # 設(shè)置pyteseract路徑
  result = pytesseract.image_to_string(image) # 圖片轉(zhuǎn)文字
  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()

更多關(guān)于python識別驗證碼圖片方法請查看下面的相關(guān)鏈接

相關(guān)文章

  • python 函數(shù)的缺省參數(shù)使用注意事項分析

    python 函數(shù)的缺省參數(shù)使用注意事項分析

    這篇文章主要介紹了python 函數(shù)的缺省參數(shù)使用注意事項,結(jié)合實例形式分析了Python函數(shù)缺省參數(shù)的使用方法與操作注意事項,需要的朋友可以參考下
    2019-09-09
  • 一篇文章讀懂Python賦值與拷貝

    一篇文章讀懂Python賦值與拷貝

    本文給大家分享一篇文章帶領(lǐng)大家讀懂python賦值與拷貝的知識,感興趣的朋友一起看看吧
    2018-04-04
  • 零基礎(chǔ)學(xué)python應(yīng)該從哪里入手

    零基礎(chǔ)學(xué)python應(yīng)該從哪里入手

    在本篇文章里小編給大家分享的是一篇關(guān)于零基礎(chǔ)學(xué)python應(yīng)該從哪里入手的相關(guān)基礎(chǔ)內(nèi)容,需要的朋友們可以參考下。
    2020-08-08
  • Python數(shù)據(jù)結(jié)構(gòu)與算法中的棧詳解(1)

    Python數(shù)據(jù)結(jié)構(gòu)與算法中的棧詳解(1)

    這篇文章主要為大家詳細(xì)介紹了Python中的棧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Python模擬用戶登錄驗證

    Python模擬用戶登錄驗證

    這篇文章主要為大家詳細(xì)介紹了Python模擬用戶登錄驗證的相關(guān)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Python利用PyExecJS庫執(zhí)行JS函數(shù)的案例分析

    Python利用PyExecJS庫執(zhí)行JS函數(shù)的案例分析

    這篇文章主要介紹了Python利用PyExecJS庫執(zhí)行JS函數(shù),本文通過案例分析給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Django開發(fā)中復(fù)選框用法示例

    Django開發(fā)中復(fù)選框用法示例

    這篇文章主要介紹了Django開發(fā)中復(fù)選框用法,結(jié)合實例形式分析了Django基于ajax的復(fù)選框遍歷、提交及后臺數(shù)據(jù)庫查詢等相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • Python+Pygame實戰(zhàn)之24點游戲的實現(xiàn)

    Python+Pygame實戰(zhàn)之24點游戲的實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了如何利用Python和Pygame實現(xiàn)24點小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • python實現(xiàn)貪吃蛇游戲

    python實現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Spectral?clustering譜聚類算法的實現(xiàn)代碼

    Spectral?clustering譜聚類算法的實現(xiàn)代碼

    譜聚類是從圖論中演化出來的算法,它的主要思想是把所有的數(shù)據(jù)看做空間中的點,這些點之間可以用邊連接起來,今天通過本文給大家介紹Spectral?clustering譜聚類算法的實現(xiàn),感興趣的朋友一起看看吧
    2022-04-04

最新評論

丹江口市| 北辰区| 黄骅市| 贵南县| 威远县| 肇东市| 项城市| 北海市| 高雄县| 晋州市| 海原县| 红桥区| 福贡县| 临沧市| 军事| 宣化县| 吉木乃县| 望江县| 鄂托克前旗| 弥勒县| 辉县市| 林甸县| 武强县| 宜川县| 三明市| 德惠市| 怀柔区| 博罗县| 溧阳市| 英超| 濉溪县| 当涂县| 贞丰县| 汉川市| 甘肃省| 田东县| 辽中县| 横峰县| 大港区| 德州市| 肇庆市|