예제 #1
0
def getcode(request):
    # 自己画图片。初始化画笔和画布
    mode = 'RGB'
    size = (180, 60)
    # 调取颜色
    red = get_color()
    green = get_color()
    blue = get_color()
    bg_color = (red, green, blue)
    # 准备画布
    image = Image.new(mode, size, color=bg_color)  # 准备画布
    draw = ImageDraw(image, mode=mode)  # 画笔
    # 字体
    font = ImageFont.truetype(font=settings.FONT_PATH, size=50)
    code = generate_code()  # 产生验证码

    request.session['code'] = code
    # 往画布画验证码
    # 取一个,画一个fill填充颜色
    for a in range(4):
        fill_color = (get_color(), get_color(), get_color())
        draw.text(xy=(47 * a, 0), text=code[a], font=font, fill=fill_color)

    for a in range(500):
        fill_color = (get_color(), get_color(), get_color())
        import random
        #位置
        position = (random.randrange(201), random.randrange(81))
        # 画点
        draw.point(xy=position, fill=fill_color)

    #   字节IO
    by = BytesIO()
    image.save(by, 'png')
    return HttpResponse(by.getvalue(), content_type='image/png')
예제 #2
0
def get_code(request):
    #初始化画步,初始化画笔
    mode = "RGB"
    size = (200, 100)
    red = get_color()
    green = get_color()
    blue = get_color()

    color_bg = (red, green, blue)

    image = Image.new(mode=mode, size=size, color=color_bg)
    imagedraw = ImageDraw(image, mode=mode)
    imagefont = ImageFont.truetype(settings.FONT_PATH, 70)

    verify_code = generate_code()

    request.session['verify_code'] = verify_code

    for i in range(4):
        fill = (get_color(), get_color(), get_color())
        imagedraw.text(xy=(50 * i, 0),
                       text=verify_code[i],
                       font=imagefont,
                       fill=fill)
    for i in range(1000):
        fill = (get_color(), get_color(), get_color())
        xy = (random.randrange(201), random.randrange(100))
        imagedraw.point(xy=xy, fill=fill)

    fp = BytesIO()
    image.save(fp, 'png')

    return HttpResponse(fp.getvalue(), content_type='image/png')
예제 #3
0
def get_code(request):

    mode = 'RGB'

    size = (200, 100)

    red = get_color()
    green = get_color()
    blue = get_color()
    # 随机画布的背景颜色
    color_bg = (red, green, blue)
    # 构造画布
    image = Image.new(mode=mode, size=size, color=color_bg)
    # 初始化画笔
    imagedraw =ImageDraw(image, mode=mode)
    # 设置字体和字体大小
    imagefont = ImageFont.truetype(settings.FONT_PATH, 100)

    verify_code = generate_verification_code()
    # 通过session记录这个验证码并且设置过期时间为60秒
    request.session['verifycode'] = verify_code
    request.session.set_expiry(20)
    # 随机每个验证码的颜色
    for i in range(4):
        # 随机颜色
        fill = (get_color(),get_color(),get_color())
        imagedraw.text(xy=(50*i, 0), text=verify_code[i], font=imagefont,fill=fill)
    # 生成验证码图片上的干扰点
    for i in range(1000):
        # 随机颜色
        fill = (get_color(), get_color(), get_color())
        # xy的随机坐标和画布的大小size一样 也就是画布整体都加干扰点
        xy = (random.randrange(201),random.randrange(100))
        imagedraw.point(xy=xy, fill=fill)

    # 生成验证码图片上的干扰线
    for i in range(5):
        # 随机颜色
        fill = (get_color(), get_color(), get_color())
        # xy的随机坐标和画布的大小size一样 也就是画布整体都加干扰点
        x1 = (random.randrange(200))
        x2 = (random.randrange(200))
        y1 = (random.randrange(100))
        y2 = (random.randrange(100))
        imagedraw.line(xy=(x1,y1,x2,y2),fill=fill,width=2)
    # 内存流
    fp = BytesIO()
    # 将验证码图片存在内存流中
    image.save(fp, 'png')

    return HttpResponse(fp.getvalue(), content_type='image/png')
예제 #4
0
def getcode(request):
    # 初始化画布,画笔
    mode = 'RGB'
    size = (200, 100)
    red = get_color()
    green = get_color()
    blue = get_color()
    # 三个 255 背景白色
    color_bg = (255, 255, 255)
    image = Image.new(mode=mode, size=size, color=color_bg)
    imagedraw = ImageDraw(image, mode=mode)

    imagefont = ImageFont.truetype(settings.FONT_PASH, 120)

    verify_code = generate_code()
    # 验证码内容存储到服务器
    request.session['verify_code'] = verify_code

    for i in range(4):
        fill = (get_color(), get_color(), get_color(), get_color())
        imagedraw.text(xy=(45 * i, 0),
                       text=verify_code[i],
                       font=imagefont,
                       fill=fill)
    for i in range(1000):
        fill = (get_color(), get_color(), get_color(), get_color())
        xy = (random.randrange(200), random.randrange(100))
        imagedraw.point(xy=xy, fill=fill)
        # imagedraw.line(xy=xy,fill= blue,width=99)

    fp = BytesIO()
    image.save(fp, 'png')

    return HttpResponse(fp.getvalue(), content_type='image/png')