隨機生成驗證碼

2021-09-27 03:34:42 字數 1538 閱讀 4187

# -*- 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 import image

import matplotlib.pyplot as plt

num=['0','1','2','3','4','5','6','7','8','9']

alph=['a','b','c','d','e','f','g','h','i','j'

,'l','m','n','o','p','q','r','s','t','u',

'v','w','x','y','z']

alph=['a','b','c','d','e','f','g','h','i','j'

,'l','m','n','o','p','q','r','s','t','u',

'v','w','x','y','z']

def random_captcha_text(size=4,charset=num+alph+alph):

captcha_text=

for i in range(size):

c=random.choice(charset)

return captcha_text

def gen_captcha_image():

#生成驗證碼物件

image=imagecaptcha()

#生成驗證碼字母數字的組合的字串

captcha_text=random_captcha_text()

captcha_text="".join(captcha_text)

#傳入字串,生成乙個png

catpchainfo=image.generate(captcha_text)

#開啟image物件,生成驗證碼的資訊和

captcha_image=image.open(catpchainfo)

captcha_image=np.array(captcha_image)

return captcha_text,captcha_image

if __name__=="__main__":

#生成驗證碼字元及

text,image=gen_captcha_image()

#顯示出生成的驗證碼和

f=plt.figure()

ax=f.add_subplot(111)

ax.text(1.1,1.9,text,ha='center',va='center')

plt.imshow(image)

plt.show()

執行效果圖:

驗證碼隨機生成

pip install captcha驗證碼隨機生成 python 版本 3.6 captcha 版本 0.3 from captcha.image import imagecaptcha import numpy as np import matplotlib.pyplot as plt from...

隨機生成驗證碼

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 是否加入小...