中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python中使用PIL制作并驗證圖片驗證碼

發布時間:2020-09-05 17:55:00 來源:腳本之家 閱讀:183 作者:Geekrun 欄目:開發技術

驗證碼制作

#string模塊自帶數字、字母、特殊字符變量集合,不需要我們手寫集合
import string
import random
import os
import uuid

import settings
from PIL import Image, ImageDraw, ImageColor, ImageFilter, ImageFont


class Code(object):
 # 生成隨機生成數字或字母
 def random_hexdigits(self, len=1):
 return random.sample(string.hexdigits, len)
 
 # 生成干擾字符
 def punctuation(self, len=1):
 return tuple(random.sample(string.punctuation, len))
 
 # 定義干擾字符顏色
 def random_color(self, min=64, max=255):
 return tuple((random.randint(min, max) for i in range(3)))
 
 # 生成驗證碼
 def creat_code(self, width=80, height=24, color=(192, 192, 192)):
 image = Image.new('RGB', (width, height), color)
 #建議下載幾款字體,變換下風格,我在setting粒定義了static路徑,這里就直接導入了
 font = ImageFont.truetype(os.path.join(settings.STATICPATH, 'fonts/Lora-Regular.ttf'), 20)
 draw = ImageDraw.Draw(image)
 self.fill_color(draw, image, 5)
 self.fill_dischar(draw, image, 10)
 code = self.fill_char(draw, image, 4, 10, font)
 image_name = '{}.jpeg'.format(uuid.uuid4().hex)
 image_path = os.path.join(settings.STATICPATH, 'code/{}'.format(image_name))
 print(image_path)
 image.save(image_path)
 return {'code': code, 'image_path': image_path}
 
 # 填充顏色
 def fill_color(self, draw, image, interval):
 for i in range(0, image.width, interval):
  for j in range(0, image.height, interval):
  draw.point((i, j), fill=self.random_color())
 
 # 填充驗證碼
 def fill_dischar(self, draw, image, interval):
 for i in range(0, image.width, interval):
  dis = self.punctuation()
  j = random.randrange(3, image.height - 3)
  draw.text((i, j), dis[0], fill=self.random_color(64, 255))
 
 # 填充驗證碼
 def fill_char(self, draw, image, num, interval, font):
 code = ''
 for i in range(num):
  cha = self.random_hexdigits()
  code += str(cha[0])
  j = random.randrange(0, 5)
  # print(cha)
  # print(image.width*(i/num)+interval,j)
  draw.text((image.width * (i / num) + interval, j), cha[0], fill=self.random_color(32, 127), font=font)
 return code


if __name__ == "__main__":
 code = Code()
 print(code.creat_code())

flask路由配置

import os
from flask import Flask, Response
from flask import render_template
from utils.code import Code

app = Flask(__name__)


@app.route('/')
def Register():
 return render_template('verify.html')


@app.route('/codes/')
def code():
 infor = Code().creat_code()
 image_path = infor["image_path"]
 code = infor['code']
 
 print(image_path)
 with open(image_path, 'rb') as f:
  image_content = f.read()
 os.remove(image_path)
 return Response(image_content, mimetype='jpeg')


if __name__ == '__main__':
 app.run(debug=True)

前端配置

 <div class='form-row'>
   <form id="email_register_form" method="post" autocomplete="off">
    <div class="form-group ">
     <label>郵&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱</label>
     <input type="text" id="id_email" name="email" value="None" placeholder="請輸入您的郵箱地址"/>
    </div>
    <div class="form-group ">
     <label>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;碼</label>
     <input type="password" id="id_password" name="password" value="None" placeholder="請輸入6-20位非中文字符密碼"/>
    </div>
    <div class="form-group captcha1 ">
     <label>驗&nbsp;證&nbsp;碼</label>
     <img src="/codes/" alt="captcha" title="點擊切換" class="captcha"
       onclick="this.src='codes/?'+Math.random()"/>
     <input autocomplete="off" id="id_captcha_1" name="captcha_1" height="30px" type="text">
    </div>
    <input class="btn btn-green" id="jsEmailRegBtn" type="submit" value="注冊"/>
   </form>

源碼分享:https://github.com/geekdick/pythonDemo/tree/master/verify

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

墨脱县| 闸北区| 临清市| 天峻县| 尼勒克县| 靖远县| 增城市| 招远市| 搜索| 封开县| 太仆寺旗| 平安县| 建瓯市| 日土县| 绥滨县| 长顺县| 耿马| 崇明县| 陆丰市| 广安市| 基隆市| 四川省| 阿瓦提县| 凯里市| 宜丰县| 五华县| 禹州市| 太原市| 天祝| 喀什市| 万盛区| 嘉禾县| 无为县| 高台县| 望城县| 泰来县| 文昌市| 正蓝旗| 镇巴县| 福鼎市| 堆龙德庆县|