亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2022年01月25日 14:13:53   作者:程序猿李巡天  
怎么用python繞驗證碼是個令人頭禿的事情,這篇文章將為大家詳細(xì) 介紹如何利用Python生成隨機的驗證碼,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起試試

最近感覺被大數(shù)據(jù)定義成機器人了,隨便看個網(wǎng)頁都跳驗證碼。

怎么用python繞驗證碼是個令人頭禿的事情,

我投降!那么今天手把手教大家如何寫驗證碼,去為難別人,讓他們頭禿。

說錯了,其實就是教大家如何通過python代碼去生成驗證碼~~

1.先搞環(huán)境

1.我們需要你電腦有python3.4以上的版本

2.pip安裝PIL包

pip install pillow

3.默念一遍"人生苦短,我用python",之后打開IDLE開始碼代碼!

2.開始碼代碼

1. 確定畫布大小和背景色

# 導(dǎo)入相關(guān)的繪畫模塊
from PIL import Image, ImageDraw, ImageFont

# 設(shè)置背景色
bg_color = (100, 100, 255)

#設(shè)置畫布長寬(像素)
width = 400
height = 100

# 通過設(shè)置生成新的畫布
im = Image.new('RGB',(width,height),bg_color)
# 展示畫布
im.show()

在這 bg_color 背景色的設(shè)置是用 RGB 顏色標(biāo)準(zhǔn)去設(shè)置的,如果你不喜歡這個背景色可以自己調(diào)一下。

“常見的RGB顏色

運行代碼后:

2. 往背景布上畫字符

先上代碼

from PIL import Image, ImageDraw, ImageFont


# 省略第一步的代碼

# 創(chuàng)建畫筆對象
draw = ImageDraw.Draw(im)
# 驗證碼文本
string = 'MSBC'
# 構(gòu)造字體對象
font = ImageFont.truetype('./ziti.ttf', 90)
# font = ImageFont.load_default().font
# 構(gòu)造字體顏色
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ù)是設(shè)置字體,我這有下載一個ttf的字體文件所以可以用它,如果沒有指定的字體文件可以使用默認(rèn)的 # font = ImageFont.load_default().font;

第二個參數(shù)是繪制字體的大小,因為我們畫布是400x100的 所以我們?yōu)榱嗣烙^就把字體設(shè)成90x90的尺寸。

# 構(gòu)造字體顏色
fontcolor = (255, 100, 100)

字體文本的顏色,參照第一步畫布的 RGB 設(shè)置。

# 繪制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ù) 寫字的坐標(biāo);

第二個參數(shù) 要寫的字;

第三個參數(shù) 字的顏色(上面構(gòu)造過了,你也可以設(shè)成一字一色)。

代碼跑一下看成果:

效果還行,就是總覺得少了點什么?

3. 加干擾

既然是驗證碼,肯定要稍微難識別,上面那個那么傻白甜的驗證碼是怎么回事??

這一步我們需要導(dǎo)入 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

別忘了導(dǎo)入 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 變量是畫干擾點的坐標(biāo)值

fill 變量是噪點的顏色,還是 RGB 標(biāo)準(zhǔn)的

draw.point 畫點的動作

“這個for循環(huán)的次數(shù)越多,畫布上噪點也會相應(yīng)增多。

跑一下代碼看看噪點的效果如何:

感覺還是有點傻白甜,我們來循環(huán)1000次的試試:

10000次!

夠了。

4. 加入更多的干擾

這一步我將各個參數(shù)結(jié)合 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)

# 構(gòu)造字體對象
font = ImageFont.truetype('./ziti.ttf', 100)
# font = ImageFont.load_default().font
# 構(gòu)造字體顏色
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()

我把字體顏色,噪點顏色,文本位置都結(jié)合了random模塊,效果圖如下:

5. 驗證碼 + 隨機字符

這一步,我們需要把上面的代碼封裝到函數(shù)中,大致把上面代碼重構(gòu)成:

# 使用for循環(huán)生成文本字符

# 生成驗證碼圖片的函數(shù),參數(shù)就是上面生成的文本

# 調(diào)用生成驗證碼圖片函數(shù)

重構(gòu)后:

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)

? ? # 構(gòu)造字體對象
? ? font = ImageFont.truetype('./ziti.ttf', 100)
? ? # font = ImageFont.load_default().font
? ? # 構(gòu)造字體顏色
? ? 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ā)的登錄操作,和訓(xùn)練驗證碼識別的神經(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)

? ? # 構(gòu)造字體對象
? ? font = ImageFont.truetype('./ziti.ttf', 100)
? ? # font = ImageFont.load_default().font
? ? # 構(gòu)造字體顏色
? ? 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} 驗證碼生成成功?。?)

最后再跑一下:

部分驗證碼展示:

注:如果再將本文中的代碼進(jìn)行變形或改寫,可能會得到更五花八門的驗證碼,怎么發(fā)揮就看屏幕錢你的了。

以上就是利用Python生成隨機驗證碼詳解的詳細(xì)內(nèi)容,更多關(guān)于Python驗證碼的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python OpenCV學(xué)習(xí)筆記

    python OpenCV學(xué)習(xí)筆記

    這篇文章主要介紹了python OpenCV的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用python的opencv,感興趣的朋友可以了解下
    2021-03-03
  • Tensorflow卷積實現(xiàn)原理+手寫python代碼實現(xiàn)卷積教程

    Tensorflow卷積實現(xiàn)原理+手寫python代碼實現(xiàn)卷積教程

    這篇文章主要介紹了Tensorflow卷積實現(xiàn)原理+手寫python代碼實現(xiàn)卷積教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • python unix時間戳轉(zhuǎn)換毫秒的實現(xiàn)

    python unix時間戳轉(zhuǎn)換毫秒的實現(xiàn)

    Unix時間戳是一種常見的時間表示方式,本文主要介紹了python unix時間戳轉(zhuǎn)換毫秒的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • Python實現(xiàn)讀取大量Excel文件并跨文件批量計算平均值

    Python實現(xiàn)讀取大量Excel文件并跨文件批量計算平均值

    這篇文章主要為大家詳細(xì)介紹了如何利用Python語言,實現(xiàn)對多個不同Excel文件進(jìn)行數(shù)據(jù)讀取與平均值計算的方法,感興趣的可以了解一下
    2023-02-02
  • python自定義分頁器的實現(xiàn)

    python自定義分頁器的實現(xiàn)

    這篇文章主要介紹了python自定義分頁器的實現(xiàn),通過自定義分頁器封裝展開主題并對其實用方法簡單介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • 深入解析python返回函數(shù)和匿名函數(shù)

    深入解析python返回函數(shù)和匿名函數(shù)

    這篇文章主要介紹了python返回函數(shù)和匿名函數(shù)的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • python?共現(xiàn)矩陣的實現(xiàn)代碼

    python?共現(xiàn)矩陣的實現(xiàn)代碼

    這篇文章主要介紹了python?共現(xiàn)矩陣的實現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • numpy.meshgrid()理解(小結(jié))

    numpy.meshgrid()理解(小結(jié))

    這篇文章主要介紹了numpy.meshgrid()理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python 利用pandas和mysql-connector獲取Excel數(shù)據(jù)寫入到MySQL數(shù)據(jù)庫

    Python 利用pandas和mysql-connector獲取Excel數(shù)據(jù)寫入到MySQL數(shù)據(jù)庫

    在實際應(yīng)用中,我們可能需要將Excel表格中的數(shù)據(jù)導(dǎo)入到MySQL數(shù)據(jù)庫中,以便于進(jìn)行進(jìn)一步的數(shù)據(jù)分析和處理,本文將介紹如何使用Python將Excel表格中的數(shù)據(jù)插入到MySQL數(shù)據(jù)庫中,需要的朋友可以參考下
    2023-10-10
  • 解決Django的request.POST獲取不到內(nèi)容的問題

    解決Django的request.POST獲取不到內(nèi)容的問題

    今天小編就為大家分享一篇解決Django的request.POST獲取不到內(nèi)容的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05

最新評論