python3使用pytesseract進行驗證碼識別


 

pytesseract介紹

1.Python-tesseract是一個基於google's Tesseract-OCR的獨立封裝包;

2.Python-tesseract功能是識別圖片文件中文字,並作為返回參數返回識別結果;

3.Python-tesseract默認支持tiff、bmp格式圖片,只有在安裝PIL之后,才能支持jpeg、gif、png等其他圖片格式

 

pytesseract安裝

1.Python-tesseract支持python2.5及更高版本;

2.Python-tesseract需要安裝PIL(Python Imaging Library) ,來支持更多的圖片格式:

pip install pillow、pip install PIL

3.Python-tesseract需要安裝tesseract-ocr安裝包:Windows安裝Tesseract-OCR 4.00並配置環境變量

4.安裝pytesseract:pip install pytesseract

 

pytesseract使用

使用步驟

> try:
> import Image
> except ImportError:
> from PIL import Image
> import pytesseract
> print(pytesseract.image_to_string(Image.open('test.png')))
> print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))

 

識別二維碼

clipboard.png

import pytesseract
from PIL import Image

image = Image.open("code.png")
code = pytesseract.image_to_string(image)
print(code)#結果:6067

 

爬蟲識別網站二維碼

import pytesseract
from PIL import Image
import requests
 
def Vercode():
    url = "http://www.xxxx"
    header = {"user_agent":"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"}
    r =requests.get(url,headers=header,timeout=5)
    with open('vcode.jpg','wb') as pic:
        pic.write(r.content)
    im = pytesseract.image_to_string(Image.open('vcode.jpg'))
    im = im.replace(' ', '')
    if im != '':
        return im
    else:
        return Vercode()
print Vcode()

 

refer:

pyhton驗證碼識別

python下調用pytesseract識別某網站驗證碼


免責聲明!

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



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