python識別驗證碼——一般的數字加字母驗證碼識別


轉自:https://www.cnblogs.com/MrRead/p/7656800.html

 

1、驗證碼的識別是有針對性的,不同的系統、應用的驗證碼區別有大有小,只要處理好圖片,利用好pytesseract,一般的驗證碼都可以識別

2、我在識別驗證碼的路上走了很多彎路,重點應該放在怎么把圖片處理成這個樣子,方便pytesseract的識別,以提高成功率

3、原圖為:

思想過程:

①不要盲目的去直接用代碼識別,識別不出來就懷疑代碼有問題或者pytesseract不好用:

   先將驗證碼用圖片處理工具處理,一步步得到理想圖片,記住處理過程,將處理后的圖片直接用pytesseract識別,代碼如下:

# -*- coding: UTF-8 -*-、
import Image
import pytesseract
im = Image.open('31.png')
aa = pytesseract.image_to_string(out)
print aa

②確定圖片可以識別后,開始用代碼復現你的圖片處理過程

復制代碼
# -*- coding: UTF-8 -*_
from PIL import Image
from pytesseract import *
import PIL.ImageOps
def initTable(threshold=140):
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    return table

im = Image.open('31.png')
#圖片的處理過程
im = im.convert('L')
binaryImage = im.point(initTable(), '1')
im1 = binaryImage.convert('L')
im2 = PIL.ImageOps.invert(im1)
im3 = im2.convert('1')
im4 = im3.convert('L')
#將圖片中字符裁剪保留
box = (30,10,90,28) 
region = im4.crop(box)  
#將圖片字符放大
out = region.resize((120,38)) 
asd = pytesseract.image_to_string(out)
print asd
print (out.show())
復制代碼

先將圖片轉換為L模式

然后去噪

反轉顏色

將重要部分裁剪放大

輸出結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM