欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Python生成隨機驗證碼詳解

 更新時間:2022年01月25日 14:13:53   作者:程序猿李巡天  
怎么用python繞驗證碼是個令人頭禿的事情,這篇文章將為大家詳細 介紹如何利用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驗證碼的資料請關注腳本之家其它相關文章!

相關文章

最新評論