日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python3.6驗(yàn)證碼隨機(jī)生成

系統(tǒng) 2192 0

這里主要使用到的庫是captcha庫,下載的命令如下:

pip install captcha
驗(yàn)證碼隨機(jī)生成代碼,(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:智軒
# 鏈接:https://blog.csdn.net/bysjlwdx/
?
?
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: 驗(yàn)證碼的候選集
? ? :param captcha_size: 生成驗(yàn)證碼中元素個(gè)數(shù)
? ? :return: 返回隨機(jī)挑選的captcha_size個(gè)元素組成的列表
? ? '''
? ? captcha_text = []
? ? for i in range(captcha_size):
? ? ? ? c = random.choice(char_set)
? ? ? ? captcha_text.append(c)
? ? return captcha_text
?
?
def gen_captcha_text_and_image():
? ? '''
? ? 把 random_captcha_text() 函數(shù)所生成列表中的元素組成字符串,
? ? 傳入到實(shí)例的方法中,生成對應(yīng)的圖片
? ? :return: captcha_text為標(biāo)簽,captcha_image為圖片
? ? '''
? ? image = ImageCaptcha()
?
? ? captcha_text = random_captcha_text()
? ? captcha_text = ''.join(captcha_text) ?# 把列表中的所有元素組成一個(gè)字符串
?
? ? 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()
? ? # 顯示驗(yàn)證碼圖片
? ? fig = plt.figure()
? ? fig.text(0.1, 0.9, text, ha='center', va='center')
? ? plt.imshow(image)
? ? plt.show()
?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 壤塘县| 雅江县| 靖州| 霍州市| 旺苍县| 唐河县| 阿坝县| 桃江县| 芜湖县| 城市| 昆明市| 南溪县| 浦县| 东阳市| 班戈县| 蓬莱市| 白河县| 桦川县| 会东县| 石家庄市| 阳朔县| 崇信县| 黎城县| 句容市| 青海省| 确山县| 雷山县| 西和县| 周至县| 西宁市| 麻江县| 南岸区| 溧水县| 鄄城县| 杭锦后旗| 同德县| 平原县| 信丰县| 新泰市| 鲜城| 江山市|