美文网首页
python生成验证码

python生成验证码

作者: linghugoogle | 来源:发表于2018-05-02 22:59 被阅读7次

    一、效果

    随机生成验证码

    二、代码

    from PIL import Image, ImageDraw, ImageFont, ImageFilter
    import random
    
    
    # random character
    def rndChar():
        return chr(random.randint(65, 90))
    
    
    # random color 1
    def rndColor1():
        return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
    
    
    # random color 2
    def rndColor2():
        return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
    
    
    # 240 * 60
    width = 60 * 4
    height = 60
    image = Image.new('RGB', (width, height), (255, 255, 255))
    
    # create Font object
    myfont = ImageFont.truetype('C:/Windows/Fonts/Calibri.ttf', 36)
    
    # create Draw object
    draw = ImageDraw.Draw(image)
    
    # fill in every pixel
    for x in range(width):
        for y in range(height):
            draw.point((x, y), fill=rndColor1())
    
        # output the text
    for t in range(4):
        tmpChar=rndChar()
        print(tmpChar)
        draw.text((60 * t + 10, 10), tmpChar, font=myfont, fill=rndColor2())
    
    # fuzzy
    image = image.filter(ImageFilter.BLUR)
    image.save('test.jpg', 'jpeg')
    

    相关文章

      网友评论

          本文标题:python生成验证码

          本文链接:https://www.haomeiwen.com/subject/wayhrftx.html