Exemple #1
0
def get_img(code):
    """
    获取验证图片
    """
    img_file = None
    try:
        background_color = (255, 255, 255)
        text_color = random.choice(
            ((27, 78, 181), (22, 163, 35), (214, 36, 7)))  # blue, green, red
        size_y = 44
        image_data = captchaimage.create_image(configs["kxfont"], 22, size_y,
                                               code)
        size_x = len(image_data) / size_y
        mask_im = Image.frombytes("L", (size_x, size_y), image_data)
        img_file = cStringIO.StringIO()
        target_im = Image.new("RGB", (size_x, size_y), text_color)
        target_im.paste(background_color, (0, 0), mask_im)
        target_im.save(img_file, "PNG")
        img = img_file.getvalue()
    except Exception as ex:
        logger.error(traceback.format_exc())
    finally:
        if img_file:
            img_file.close()
    return img
Exemple #2
0
def get_captcha_image(code):
    size_y = 32
    image_data = captchaimage.create_image(
        "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 28, size_y, code)
    file = cStringIO.StringIO()
    Image.fromstring(
        "L", (len(image_data) / size_y, size_y), image_data).save(
        file, "JPEG", quality = 30)
    return file.getvalue()
Exemple #3
0
def get_captcha_image(code):
    size_y = 32
    image_data = captchaimage.create_image(
        "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 28, size_y, code)
    file = cStringIO.StringIO()
    Image.fromstring(
        "L", (len(image_data) / size_y, size_y), image_data).save(
        file, "JPEG", quality = 30)
    return file.getvalue()
Exemple #4
0
def get_captcha_image(code):
    size_y = 32
    image_data = captchaimage.create_image(
        FONT_FILE, 28, size_y, code)
    file = cStringIO.StringIO()
    Image.fromstring(
        "L", (len(image_data) / size_y, size_y), image_data).save(
        file, "JPEG", quality = 30)
    return file.getvalue()
Exemple #5
0
def captcha(size_y=40):
    text = ''.join(choice(CHARSET) for i in xrange(5))
    image_data = captchaimage.create_image(FONT_PATH, 30, size_y, text)

    image = Image.frombytes("L", (len(image_data) / size_y, size_y),
                            image_data)

    f = StringIO()
    image.save(f, "PNG")
    return text, b64encode(f.getvalue())
def captcha(size_y=40):
    text=''.join(choice(CHARSET) for i in xrange(5))
    image_data = captchaimage.create_image(
           FONT_PATH,30,size_y,
           text
           )

    image = Image.fromstring(
           "L", (len(image_data) / size_y, size_y), image_data)

    f = StringIO()
    image.save(f,"PNG")
    return text, b64encode(f.getvalue())