python簡單驗證碼識別的實現(xiàn)過程

轉(zhuǎn)載: https://www.zhangshengrong.com/p/yOXDZwz3aB/

demo :
import pytesseract
from PIL import Image
image = Image.open("captcha.png")
print(pytesseract.image_to_string(image))
=================================================
=================================================中文識別
import pytesseract
from PIL import Image
image = Image.open("00.jpg")
print(pytesseract.image_to_string(image,lang='chi_sim'))

有時候文本識別率并不高,建議圖像識別前,先對圖像進行灰度化和 二值化

import pytesseract
from PIL import Image
file = r"00.jpg"
 
# 先對圖像進行灰度化和 二值化
image = Image.open(file)
Img = image.convert('L')   # 灰度化
#自定義灰度界限,這里可以大于這個值為黑色,小于這個值為白色。threshold可根據(jù)實際情況進行調(diào)整(最大可為255)。
threshold = 180
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)
photo = Img.point(table, '1')  #圖片二值化
#保存處理好的圖片
photo.save('01.jpg')
 
image = Image.open('01.jpg')
# 解析圖片,lang='chi_sim'表示識別簡體中文,默認為English
# 如果是只識別數(shù)字,可再加上參數(shù)config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789'
content = pytesseract.image_to_string(image, lang='chi_sim')
print(content)
  1. 實戰(zhàn)案例–實現(xiàn)古詩文網(wǎng)驗證碼自動識別登錄

    import pytesseract
    from PIL import Image
    from selenium import webdriver

    def save_captcha(path):
    driver = webdriver.Chrome() # 創(chuàng)建瀏覽器對象
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url=url)
    image = driver.find_element_by_id('imgCode')
    image.screenshot(path)
    return driver

    def recognize_captcha(captcha_path):
    captcha = Image.open(captcha_path) # 打開圖片
    grap = captcha.convert('L') # 對圖片進行灰度化處理
    data = grap.load() # 將圖片對象加載成數(shù)據(jù)
    w, h = captcha.size # 獲取圖片的大小(寬度,高度)
    # 圖片二值化處理
    for x in range(w):
    for y in range(h):
    if data[x, y] < 140:
    data[x, y] = 0
    else:
    data[x, y] = 255
    code = pytesseract.image_to_string(grap) # 對圖片進行識別
    return code

    def login(driver, code):
    flag = True
    email = '1242931802@qq.com' # 注冊的古詩文網(wǎng)賬號和密碼
    password = 'xxxx'
    try:
    driver.find_element_by_id('email').send_keys(email)
    driver.find_element_by_id('pwd').send_keys(password)
    driver.find_element_by_id('code').send_keys(code)
    driver.implicitly_wait(10)
    driver.find_element_by_id('denglu').click()
    except Exception as ex:
    flag = False
    return flag

    if name == 'main':
    url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'
    captcha_path = './captcha.png'
    count = 1
    driver = save_captcha(captcha_path) # 獲取驅(qū)動
    code = recognize_captcha(captcha_path) # 獲取驗證碼
    print('識別驗證碼為:', code)
    if login(driver, code):
    driver.quit()

效果如下(有時候第一次可能識別失敗,可以寫個循環(huán)邏輯讓它多識別幾次,一般程序運行1-3次基本會識別成功):

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容