利用Python生成隨機驗證碼詳解
最近感覺被大數(shù)據(jù)定義成機器人了,隨便看個網(wǎng)頁都跳驗證碼。
怎么用python繞驗證碼是個令人頭禿的事情,
我投降!那么今天手把手教大家如何寫驗證碼,去為難別人,讓他們頭禿。
說錯了,其實就是教大家如何通過python代碼去生成驗證碼~~
1.先搞環(huán)境
1.我們需要你電腦有python3.4以上的版本
2.pip安裝PIL包
pip install pillow
3.默念一遍"人生苦短,我用python",之后打開IDLE開始碼代碼!
2.開始碼代碼
1. 確定畫布大小和背景色
# 導入相關的繪畫模塊 from PIL import Image, ImageDraw, ImageFont # 設置背景色 bg_color = (100, 100, 255) #設置畫布長寬(像素) width = 400 height = 100 # 通過設置生成新的畫布 im = Image.new('RGB',(width,height),bg_color) # 展示畫布 im.show()
在這 bg_color 背景色的設置是用 RGB 顏色標準去設置的,如果你不喜歡這個背景色可以自己調(diào)一下。
“常見的RGB顏色
運行代碼后:
2. 往背景布上畫字符
先上代碼
from PIL import Image, ImageDraw, ImageFont # 省略第一步的代碼 # 創(chuàng)建畫筆對象 draw = ImageDraw.Draw(im) # 驗證碼文本 string = 'MSBC' # 構造字體對象 font = ImageFont.truetype('./ziti.ttf', 90) # font = ImageFont.load_default().font # 構造字體顏色 fontcolor = (255, 100, 100) # 繪制4個字 draw.text((10, 2), string[0], font=font, fill=fontcolor) draw.text((110, 2), string[1], font=font, fill=fontcolor) draw.text((210, 2), string[2], font=font, fill=fontcolor) draw.text((310, 2), string[3], font=font, fill=fontcolor) #釋放畫筆 del draw #展示圖片 im.show()
代碼分析:
draw = ImageDraw.Draw(im)
在im畫布上實例化一只筆。
font = ImageFont.truetype('./ziti.ttf', 90) # font = ImageFont.load_default().font
第一個參數(shù)是設置字體,我這有下載一個ttf的字體文件所以可以用它,如果沒有指定的字體文件可以使用默認的 # font = ImageFont.load_default().font;
第二個參數(shù)是繪制字體的大小,因為我們畫布是400x100的 所以我們?yōu)榱嗣烙^就把字體設成90x90的尺寸。
# 構造字體顏色 fontcolor = (255, 100, 100)
字體文本的顏色,參照第一步畫布的 RGB 設置。
# 繪制4個字 draw.text((10, 2), string[0], font=font, fill=fontcolor) draw.text((110, 2), string[1], font=font, fill=fontcolor) draw.text((210, 2), string[2], font=font, fill=fontcolor) draw.text((310, 2), string[3], font=font, fill=fontcolor)
這里 draw.text 函數(shù),顧名思義就是開始拿畫起畫筆開始寫字,
第一個參數(shù) 寫字的坐標;
第二個參數(shù) 要寫的字;
第三個參數(shù) 字的顏色(上面構造過了,你也可以設成一字一色)。
代碼跑一下看成果:
效果還行,就是總覺得少了點什么?
3. 加干擾
既然是驗證碼,肯定要稍微難識別,上面那個那么傻白甜的驗證碼是怎么回事??
這一步我們需要導入 random 模塊,因為干擾是不規(guī)則隨機生成的。
import random from PIL import Image, ImageDraw, ImageFont # 省略第一步代碼 # 省略第二步代碼 #使用point函數(shù)繪制噪點 for i in range(0, 100): ? ? xy = (random.randrange(0, width), random.randrange(0, height)) ? ? fill = (random.randrange(0, 255), 255, random.randrange(0, 255)) ? ? draw.point(xy, fill=fill) ? ?? #釋放畫筆 del draw ?? im.show()
代碼分析:
import random
別忘了導入 random 模塊
for i in range(0, 100): xy = (random.randrange(0, width), random.randrange(0, height)) fill = (255, 255, 255) draw.point(xy, fill=fill)
一個循環(huán)100次的 for 循環(huán),
xy 變量是畫干擾點的坐標值
fill 變量是噪點的顏色,還是 RGB 標準的
draw.point 畫點的動作
“這個for循環(huán)的次數(shù)越多,畫布上噪點也會相應增多。
跑一下代碼看看噪點的效果如何:
感覺還是有點傻白甜,我們來循環(huán)1000次的試試:
10000次!
夠了。
4. 加入更多的干擾
這一步我將各個參數(shù)結合 random 模塊,使我們的驗證碼更難辨別!
import random from PIL import Image, ImageDraw, ImageFont bg_color = (random.randrange(20, 120), random.randrange(20, 120), random.randrange(150, 255)) width = 400 height = 100 im = Image.new('RGB',(width,height),bg_color) # 創(chuàng)建畫筆對象 draw = ImageDraw.Draw(im) # 構造字體對象 font = ImageFont.truetype('./ziti.ttf', 100) # font = ImageFont.load_default().font # 構造字體顏色 fontcolor = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)) # 繪制4個字 string = 'MSBC' draw.text((random.randint(10, 30), (random.randint(0, 10))), string[0], font=font, fill=fontcolor) draw.text((random.randint(90, 130), (random.randint(0, 10))), string[1], font=font, fill=fontcolor) draw.text((random.randint(180, 230), (random.randint(0, 10))), string[2], font=font, fill=fontcolor) draw.text((random.randint(270, 330), (random.randint(0, 10))), string[3], font=font, fill=fontcolor) #調(diào)用畫筆的point()函數(shù)繪制噪點 for i in range(0, 10000): ? ? xy = (random.randrange(0, width), random.randrange(0, height)) ? ? fill = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)) ? ? draw.point(xy, fill=fill) #釋放畫筆 del draw im.show()
我把字體顏色,噪點顏色,文本位置都結合了random模塊,效果圖如下:
5. 驗證碼 + 隨機字符
這一步,我們需要把上面的代碼封裝到函數(shù)中,大致把上面代碼重構成:
# 使用for循環(huán)生成文本字符 # 生成驗證碼圖片的函數(shù),參數(shù)就是上面生成的文本 # 調(diào)用生成驗證碼圖片函數(shù)
重構后:
import random from PIL import Image, ImageDraw, ImageFont string = '' #隨機選取4個值作為驗證碼 rand_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' for i in range(0, 4): ? ? string += rand_str[random.randrange(0, len(rand_str))] def gen_verify_img(text): ? ? bg_color = (random.randrange(20, 120), random.randrange(20, 120), random.randrange(150, 255)) ? ? width = 400 ? ? height = 100 ? ? im = Image.new('RGB',(width,height),bg_color) ? ? # 創(chuàng)建畫筆對象 ? ? draw = ImageDraw.Draw(im) ? ? # 構造字體對象 ? ? font = ImageFont.truetype('./ziti.ttf', 100) ? ? # font = ImageFont.load_default().font ? ? # 構造字體顏色 ? ? fontcolor = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)) ? ? # 繪制4個字 ? ? draw.text((random.randint(10, 30), (random.randint(0, 10))), string[0], font=font, fill=fontcolor) ? ? draw.text((random.randint(90, 130), (random.randint(0, 10))), string[1], font=font, fill=fontcolor) ? ? draw.text((random.randint(180, 230), (random.randint(0, 10))), string[2], font=font, fill=fontcolor) ? ? draw.text((random.randint(270, 330), (random.randint(0, 10))), string[3], font=font, fill=fontcolor) ? ? #調(diào)用畫筆的point()函數(shù)繪制噪點 ? ? for i in range(0, 10000): ? ? ? ? xy = (random.randrange(0, width), random.randrange(0, height)) ? ? ? ? fill = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)) ? ? ? ? draw.point(xy, fill=fill) ? ? #釋放畫筆 ? ? del draw ? ? im.show() # 調(diào)用函數(shù) gen_verify_img(string)
把原先代碼中的 string 變量提到了函數(shù)外,把它變成函數(shù)需要傳入的參數(shù),
再用 for 循環(huán),隨機選出4個字符。
string = '' #隨機選取4個值作為驗證碼 rand_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' for i in range(0, 4): string += rand_str[random.randrange(0, len(rand_str))]
代碼再跑一下:
上面的驗證碼是 DZNO 還是 DZN0 ?
6. 驗證碼保存本地(選)
在web開發(fā)的登錄操作,和訓練驗證碼識別的神經(jīng)運算中,都需要大量的驗證碼圖片。
所以需要把大量的驗證碼圖片文件,我們將批量驗證碼保存到本地。
完整代碼:
import random from PIL import Image, ImageDraw, ImageFont def gen_verify_img(text): ? ? bg_color = (random.randrange(20, 120), random.randrange(20, 120), random.randrange(150, 255)) ? ? width = 400 ? ? height = 100 ? ? im = Image.new('RGB',(width,height),bg_color) ? ? # 創(chuàng)建畫筆對象 ? ? draw = ImageDraw.Draw(im) ? ? # 構造字體對象 ? ? font = ImageFont.truetype('./ziti.ttf', 100) ? ? # font = ImageFont.load_default().font ? ? # 構造字體顏色 ? ? fontcolor = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)) ? ? # 繪制4個字 ? ? draw.text((random.randint(10, 30), (random.randint(0, 10))), string[0], font=font, fill=fontcolor) ? ? draw.text((random.randint(90, 130), (random.randint(0, 10))), string[1], font=font, fill=fontcolor) ? ? draw.text((random.randint(180, 230), (random.randint(0, 10))), string[2], font=font, fill=fontcolor) ? ? draw.text((random.randint(270, 330), (random.randint(0, 10))), string[3], font=font, fill=fontcolor) ? ? #調(diào)用畫筆的point()函數(shù)繪制噪點 ? ? for i in range(0, 10000): ? ? ? ? xy = (random.randrange(0, width), random.randrange(0, height)) ? ? ? ? fill = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)) ? ? ? ? draw.point(xy, fill=fill) ? ? #釋放畫筆 ? ? del draw ? ? # im.show() ? ? im.save(f'./{text}.png','png') for i in range(100): ? ? string = '' ? ? #隨機選取4個值作為驗證碼 ? ? rand_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ? ? for i in range(0, 4): ? ? ? ? string += rand_str[random.randrange(0, len(rand_str))] ? ? gen_verify_img(string) ? ? print(f'{string} 驗證碼生成成功!!')
最后再跑一下:
部分驗證碼展示:
注:如果再將本文中的代碼進行變形或改寫,可能會得到更五花八門的驗證碼,怎么發(fā)揮就看屏幕錢你的了。
以上就是利用Python生成隨機驗證碼詳解的詳細內(nèi)容,更多關于Python驗證碼的資料請關注腳本之家其它相關文章!
相關文章
Tensorflow卷積實現(xiàn)原理+手寫python代碼實現(xiàn)卷積教程
這篇文章主要介紹了Tensorflow卷積實現(xiàn)原理+手寫python代碼實現(xiàn)卷積教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python unix時間戳轉(zhuǎn)換毫秒的實現(xiàn)
Unix時間戳是一種常見的時間表示方式,本文主要介紹了python unix時間戳轉(zhuǎn)換毫秒的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01Python實現(xiàn)讀取大量Excel文件并跨文件批量計算平均值
這篇文章主要為大家詳細介紹了如何利用Python語言,實現(xiàn)對多個不同Excel文件進行數(shù)據(jù)讀取與平均值計算的方法,感興趣的可以了解一下2023-02-02Python 利用pandas和mysql-connector獲取Excel數(shù)據(jù)寫入到MySQL數(shù)據(jù)庫
在實際應用中,我們可能需要將Excel表格中的數(shù)據(jù)導入到MySQL數(shù)據(jù)庫中,以便于進行進一步的數(shù)據(jù)分析和處理,本文將介紹如何使用Python將Excel表格中的數(shù)據(jù)插入到MySQL數(shù)據(jù)庫中,需要的朋友可以參考下2023-10-10解決Django的request.POST獲取不到內(nèi)容的問題
今天小編就為大家分享一篇解決Django的request.POST獲取不到內(nèi)容的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05