zoukankan      html  css  js  c++  java
  • Python3实现生成验证码图片

    import random
    from PIL import Image, ImageFont, ImageDraw
    from io import BytesIO
    from ttt import settings
    import os
    import base64

    PATH = os.path.join(settings.D_APP_ROOT, 'apps/membership/static/noisy.ttf')  #  字体
    LEN_VERIFY = 4

    def get_verify():

      verify_len = LEN_VERIFY
      weight = 108
      hight = 41
      # 大写字母,小写字母,数字
      txt_list = [48, 49, 50, 51, 52, 53, 54, 55, 56, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
      81,
      82, 83, 84, 85, 86, 87, 88, 89, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
      111,
      112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
      image = Image.new('RGBA', (weight, hight), (255, 255, 255))
      font = ImageFont.truetype(PATH, 20)
      draw = ImageDraw.Draw(image)
      # 填充背景
      for x in range(weight):
      for y in range(hight):
      draw.point((x, y), fill=(200, 200, 200))
      # 生成随机验证码
      verify = ''
      for t in range(verify_len):
      text = chr(txt_list[random.randint(0, len(txt_list) - 1)])
      verify += text
      draw.text(((weight // verify_len) * t + 7, 10), text, font=font,
      fill=(random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)))
      img_buffer = BytesIO()
      image.save(img_buffer, 'PNG')
      base = base64.b64encode(img_buffer.getvalue())
      return base, verify

    # django中返回方式

    return HttpResponse(image, content_type='image/png')   

  • 相关阅读:
    存储过程的设计规则
    企业管理器里删除不需要的注册
    SQL Server 大数据量插入和索引关系
    【2011520】无法使用主机名连接数据库
    SQL Server 查看存储过程
    SQL Server dbcc inputbuffer
    如何选择行版本的隔离级别
    ObjectiveC中Selector基本概念和操作
    Objectivec的@property 详解
    objectivec 关键字和概念
  • 原文地址:https://www.cnblogs.com/tdsun/p/8386901.html
Copyright © 2011-2022 走看看