圆月山庄资源网 Design By www.vgjia.com

验证码制作

#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="/UploadFiles/2021-04-08/">

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

标签:
python,PIL,验证码

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。