zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    解释下Http请求头和常见响应状态码
    sys 模块常用方法
    os 模块常用方法
    说明os,sys模块有什么不同
    dict 的 items() 方法与 iteritems() 方法的不同?
    Python是如何进行类型转换的?
    Python中pass语句的作用是什么?
    创建一个简单tcp服务器需要的流程
    安全性
    传输数据的大小
  • 原文地址:https://www.cnblogs.com/yyx1-1/p/6081309.html
Copyright © 2011-2022 走看看