1. 程式人生 > >Python識別平臺登入驗證碼

Python識別平臺登入驗證碼

工作於運維時間長了,安全問題一直是重點。今天來研究一下簡單的驗證碼可能帶來的安全問題。

mac python2.7安裝PIL.Image模組


mac python Image PIL 
要想在python中操作圖片,比如引入PIL(Python Imaging Library)庫。
 
在python安裝第三庫時,可以使用工具easy_install或pip,我推薦使用pip這個工具。針對mac電腦,pip是不需要手動安裝,在命令列可以直接輸入pip驗證是否已經安裝。
 
若是pip沒有安裝,可以使用命令sudo easy_install pip(保證mac連線網際網路)
 
安裝pip之後,可以使用命令sudo pip install PIL來安裝操作圖片的模組了。
 
若是安裝正常,那皆大歡喜了。在我電腦安裝時,卻出現了問題。
could not find a version that satisfies the requirement PIL.(form versions:)
No matching distribution found for PIL.
 
這個是說明PIL已經找不到,其實現在已經用Pillow代替了PIL,在使用方面沒有不同,API都是相同的。
 
既然如此,咱們就直接安裝Pillow模組吧,執行sudo pip install Pillow
 
安裝這個模組時,發現它會依賴另外一個模組:multiprocessing
 
只能先把multiprocessing模組安裝好再執行上面的命令了,sudo pip install multiprocessing即可正常安裝,非常小的一個模組
 
接著再執行sudo pip install Pillow命令,就可以正常安裝模組了,自動安裝好之後,就可以正常使用了。
 
使用時需要注意的是引入模組要按照下面的方式寫(注意大小寫)
第一種:from PIL import Image
第二種:from PIL.Image(用這種方式時,下面使用時也得寫成PIL.Image.open('1.png'),個人覺得不太好看,可以在引入時修改下模組名,如from PIL.Image as image)
 

引入之後就可以正常使用了。

安裝步驟:

sudo pip install multiprocessing

sudo pip install Pillow

brew install --with-training-tools tesseract

使用:

1.命令列方式:

tesseract test.png output.txt #識別test.png的圖片,把結果放到output.txt中

2. python 程式碼:

mac-temp:py test$ cat vcode.py 
#coding=utf-8
from PIL import Image
import pytesseract

im = Image.open('./test2.png')
print pytesseract.image_to_string(im)


mac-temp:py test$ python ./vcode.py 
Hello world!

1234

以上只能識別簡單的圖片:

複雜一點的就不能識別:


所以,複雜一點的驗證碼需要做更多處理。

參考:

 https://stackoverflow.com/questions/28741563/pytesseract-no-such-file-or-directory-error

https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00140767171357714f87a053a824ffd811d98a83b58ec13000

http://blog.csdn.net/huangzhang_123/article/details/72819061