驗證碼隨機生成

2021-09-25 23:17:27 字數 1936 閱讀 2364

pip install captcha
驗證碼隨機生成**,(python 版本:3.6     captcha 版本:0.3)

from captcha.image import imagecaptcha

import numpy as np

import matplotlib.pyplot as plt

from pil import image

import random

# csdn author:jumping boy

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+alphabet+alphabet, captcha_size=4):

''':param char_set: 驗證碼的候選集

:param captcha_size: 生成驗證碼中元素個數

:return: 返回隨機挑選的captcha_size個元素組成的列表

'''captcha_text =

for i in range(captcha_size):

c = random.choice(char_set)

return captcha_text

def gen_captcha_text_and_image():

'''把 random_captcha_text() 函式所生成列表中的元素組成字串,

傳入到例項的方法中,生成對應的

:return: captcha_text為標籤,captcha_image為

'''image = imagecaptcha()

captcha_text = random_captcha_text()

captcha_text = ''.join(captcha_text) # 把列表中的所有元素組成乙個字串

captcha = image.generate(captcha_text)

captcha_image = image.open(captcha)

captcha_image = np.array(captcha_image)

return captcha_text, captcha_image

if __name__ == '__main__':

text, image = gen_captcha_text_and_image()

# 顯示驗證碼

隨機生成驗證碼

coding utf 8 created on mon sep 16 01 21 02 2019 author administrator import random from captcha.image import imagecaptcha import numpy as np from pil...

隨機生成驗證碼

private static int r private static int g private static int b private static stringbuffer buffer public static final char chars public static random ...

c 生成隨機驗證碼

public class captcha set endregion region 驗證碼字串 驗證碼字串 private string verifycodetext null 驗證碼字串 public string verifycodetext set endregion region 是否加入小...