from captcha.image import imagecaptcha # pip install captchaimport numpy as np
from pil import image
import random
import sys
import os
if not os.path.exists('captcha/images/'):
os.makedirs('captcha/images/')
number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
# alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
# alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
def random_captcha_text(char_set=number, captcha_size=4):
# 驗證碼列表
captcha_text =
for i in range(captcha_size):
# 隨機選擇
c = random.choice(char_set)
# 加入驗證碼列表
return captcha_text
# 生成字元對應的驗證碼
def gen_captcha_text_and_image():
image = imagecaptcha()
# 獲得隨機生成的驗證碼
captcha_text = random_captcha_text()
# 把驗證碼列表轉為字串
captcha_text = ''.join(captcha_text)
# 生成驗證碼
captcha = image.generate(captcha_text)
image.write(captcha_text, 'captcha/images/' + captcha_text + '.jpg') # 寫到檔案
# 數量少於10000,因為重名
num = 10000
if __name__ == '__main__':
for i in range(num):
gen_captcha_text_and_image()
sys.stdout.write('\r>> creating image %d/%d' % (i + 1, num))
sys.stdout.flush()
sys.stdout.write('\n')
sys.stdout.flush()
print("生成完畢")
驗證碼一(驗證碼生成)
根據手機好查詢密碼 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...