首页 > 编程语言 > 详细

利用python生成验证码的四中方式

时间:2019-02-14 16:35:52      阅读:180      评论:0      收藏:0      [点我收藏+]
import random
from io import BytesIO

from PIL import Image, ImageDraw, ImageFont

from django.shortcuts import render, HttpResponse

def get_random_color():
    return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))


def get_validCode_img(request):
    # 方式1:
    # with open(‘1.jpeg‘, ‘rb‘) as f:
    #     data = f.read()
    # return HttpResponse(data)

    # 方式2(在硬盘上生成、读取图片):
    # img = Image.new(‘RGB‘, (260, 34), color=get_random_color())
    #
    # with open(‘validCode.png‘, ‘wb‘) as f:
    #     img.save(f, ‘png‘)
    #
    # with open(‘validCode.png‘, ‘rb‘) as f:
    #     data = f.read()
    #
    # return HttpResponse(data)

    # 方式3(在内存中生成、读取图片):
    # img = Image.new(‘RGB‘, (260, 34), color=get_random_color())
    #
    # f = BytesIO()  # 用完之后,BytesIO会自动清掉
    # img.save(f, ‘png‘)
    # data = f.getvalue()
    #
    # return HttpResponse(data)

    # 方式4(给image加文字):
    img = Image.new(RGB, (260, 34), color=get_random_color())
    draw = ImageDraw.Draw(img)
    kumo_font = ImageFont.truetype(static/font/kumo.ttf, size=28)

    valid_code_str = ‘‘
    for i in range(5):
        random_num = str(random.randint(0, 9))
        random_low_alpha = chr(random.randint(97, 122))
        random_high_alpha = chr(random.randint(65, 90))
        random_char = random.choice([random_num, random_low_alpha, random_high_alpha])
        draw.text((i * 50 + 20, 5), random_char, get_random_color(), font=kumo_font)

        # 保存验证码字符串
        valid_code_str += random_char

    # 噪点噪线
    width = 260
    height = 34
    for i in range(5):
        x1 = random.randint(0, width)
        x2 = random.randint(0, width)
        y1 = random.randint(0, height)
        y2 = random.randint(0, height)
        draw.line((x1, y1, x2, y2), fill=get_random_color())

    for i in range(5):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color())

    # 保存验证码字符串到该用户的session
    request.session[valid_code_str] = valid_code_str

    f = BytesIO()  # 用完之后,BytesIO会自动清掉
    img.save(f, png)
    data = f.getvalue()

    return HttpResponse(data)

 

利用python生成验证码的四中方式

原文:https://www.cnblogs.com/lshedward/p/10375294.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!