12.1.圖形驗證碼生成
(1)utils/captcha/init.pygit
import random import string # Image:一個畫布 # ImageDraw:一個畫筆 # ImageFont:畫筆的字體 from PIL import Image,ImageDraw,ImageFont # pip install pillow # Captcha驗證碼 class Captcha(object): # 生成幾位數的驗證碼 number = 4 # 驗證碼圖片的寬度和高度 size = (100,30) # 驗證碼字體大小 fontsize = 25 # 加入干擾線的條數 line_number = 2 # 構建一個驗證碼源文本 SOURCE = list(string.ascii_letters) for index in range(0, 10): SOURCE.append(str(index)) #用來繪製干擾線 @classmethod def __gene_line(cls,draw,width,height): begin = (random.randint(0, width), random.randint(0, height)) end = (random.randint(0, width), random.randint(0, height)) draw.line([begin, end], fill = cls.__gene_random_color(),width=2) # 用來繪製干擾點 @classmethod def __gene_points(cls,draw,point_chance,width,height): chance = min(100, max(0, int(point_chance))) # 大小限制在[0, 100] for w in range(width): for h in range(height): tmp = random.randint(0, 100) if tmp > 100 - chance: draw.point((w, h), fill=cls.__gene_random_color()) # 生成隨機的顏色 @classmethod def __gene_random_color(cls,start=0,end=255): random.seed() return (random.randint(start,end),random.randint(start,end),random.randint(start,end)) # 隨機選擇一個字體 @classmethod def __gene_random_font(cls): fonts = [ 'msyh.ttf', 'msyhbd.ttf', 'simkai.ttf', 'simsun.ttc', 'webdings.ttf' ] font = random.choice(fonts) # print(font) return 'utils/captcha/'+font # 用來隨機生成一個字符串(包括英文和數字) @classmethod def gene_text(cls, number): # number是生成驗證碼的位數 return ''.join(random.sample(cls.SOURCE, number)) #生成驗證碼 @classmethod def gene_graph_captcha(cls): # 驗證碼圖片的寬和高 width,height = cls.size # 建立圖片 # R:Red(紅色)0-255 # G:G(綠色)0-255 # B:B(藍色)0-255 # A:Alpha(透明度) image = Image.new('RGBA',(width,height),cls.__gene_random_color(0,100)) # 驗證碼的字體 font = ImageFont.truetype(cls.__gene_random_font(),cls.fontsize) # 建立畫筆 draw = ImageDraw.Draw(image) # 生成字符串 text = cls.gene_text(cls.number) # 獲取字體的尺寸 font_width, font_height = font.getsize(text) # 填充字符串 draw.text(((width - font_width) / 2, (height - font_height) / 2),text,font= font,fill=cls.__gene_random_color(150,255)) # 繪製干擾線 for x in range(0, cls.line_number): cls.__gene_line(draw, width, height) # 繪製噪點 cls.__gene_points(draw, 10, width, height) # with open('captcha.png','wb') as fp: # image.save(fp) return (text,image)
(2)在utils/captcha文件下面放幾種字體github
(3)front/views.pyweb
@bp.route('/captcha/') def graph_captcha(): text,image = Captcha.gene_graph_captcha() out = BytesIO() image.save(out,'png') #指定格式爲png out.seek(0) #把指針指到開始位置 resp = make_response(out.read()) resp.content_type = 'image/png' return resp
(4)生成驗證碼瀏覽器
運行項目,瀏覽器訪問:http://127.0.0.1:5000/captcha/,能夠看到生成的隨機驗證碼app