• python代码 构建验证码


    1.python代码编写 (随机验证码):

     1 #coding: utf-8
     2 
     3 import Image, ImageDraw, ImageFont, ImageFilter
     4 import string, random
     5 
     6 fontPath = "/home/itcast/ace/media/"
     7 
     8 # 获得随机四个字母
     9 def getRandomChar():
    10     return [random.choice(string.letters) for _ in range(4)]
    11 
    12 # 获得颜色
    13 def getRandomColor():
    14     return (random.randint(30, 100), random.randint(30, 100), random.randint(30, 100))
    15 
    16 # 获得验证码图片
    17 def getCodePiture():
    18     width = 240
    19     height = 60
    20 
    21     # 创建画布
    22     image = Image.new('RGB', (width, height), (180,180,180))
    23     font = ImageFont.truetype(fontPath + 'simhei.ttf', 80)
    24     draw = ImageDraw.Draw(image)
    25 
    26     # 创建验证码对象
    27     code = getRandomChar()#code-> [x,A,y,U] 
    28 
    29     # 把验证码放到画布上
    30     for t in range(4):
    31         draw.text((60 * t + 10, 0), code[t], font=font, fill=getRandomColor())
    32 
    33     # 填充噪点
    34     for _ in range(random.randint(1500,3000)):
    35         draw.point((random.randint(0,width), random.randint(0,height)), fill=getRandomColor())
    36 
    37     # 模糊处理
    38 #image = image.filter(ImageFilter.BLUR)
    39 
    40     # 保存名字为验证码的图片
    41     #code = [x,y, U,a] --> xyUa.jpg
    42     image.save("".join(code) + '.jpg', 'jpeg');
    43 
    44 
    45 if __name__ == '__main__':
    46     getCodePiture()
  • 相关阅读:
    solr 重要的知识点
    Sole跟Tomcat的整合 Solr4.4 + Tomcat
    常用SQL总结
    T4生成实体和简单的CRUD操作
    磁盘分区
    salt一键部署zookeeper
    salt一键部署gcc-c++
    salt一键部署python
    linux目录文件权限的区别。
    salt一键部署jdk
  • 原文地址:https://www.cnblogs.com/yyx1-1/p/6081309.html
Copyright © 2020-2023  润新知