驗證碼的生成

2021-09-10 08:58:17 字數 1657 閱讀 8298

py的庫就是多,簡單應用,最近看了乙個驗證碼生成的簡單操作,挺有意思的,直接上**,看吧。

# coding=utf-8

import random

from pil import image, imagefont, imagedraw

# 隨機生成大小寫字母數字

def get_rand(many):

for i in range(many):

s = ''

n = random.randint(1, 2)

if n == 1:

num1 = random.randint(0, 9)

s += str(num1)

else:

n1 = random.randint(1, 2)

n2 = random.randint(1, 26)

if n1 == 1:

num1 = chr(64+n2)

s += num1

else:

num1 = chr(96+n2)

s += num1

return s

# 隨機顏色

def rnd_color():

return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

# 隨機背景顏色

def rnd_color2():

return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

# 設定影象模式尺寸顏色

width = 60*4

height = 60

# 顯示

image = image.new('rgb', (width, height), (0, 0, 0))

# 建立字型物件

font = imagefont.truetype('agencyb.ttf', 40)

# 寫入物件

draw = imagedraw.draw(image)

# 填充畫素點

for i in range(width):

for j in range(height):

draw.point((i, j), fill=rnd_color2())

draw.line((0, 0 + random.randint(0, height // 2), width, 0 + random.randint(0, height // 2)), fill=rnd_color())

draw.line((0, height - random.randint(0, height // 2), width, height - random.randint(0, height // 2)), fill=rnd_color())

draw.line((0, 0, 240, 60), width=10, fill=rnd_color())

for i in range(6):

# 寫入資訊寫入座標

draw.text((40*i+15, 10), get_rand(6), font=font, fill=rnd_color())

image.show()

驗證碼一(驗證碼生成)

根據手機好查詢密碼 return type description code for i 0 i 6 i 4位驗證碼也可以用rand 1000,9999 直接生成 將生成的驗證碼寫入session,備驗證時用 session start session verify num code 建立,定義顏色...

生成驗證碼

在此處放置使用者 以初始化頁面 bitmap image new bitmap int math.ceiling validatenum.length 12.5 22 graphics g graphics.fromimage image tryfinally region web 窗體設計器生成的...

生成驗證碼

生成驗證碼的類 using system using system.data using system.configuration using system.collections using system.web using system.web.security using system.web...