CAPTCHA.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:scripts-for-bupt 作者: Forec 项目源码 文件源码
def make( numbers, width = 400, height = 200):
    strs = ''.join(random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', numbers))
    im = Image.new( 'RGB', (width, height ), (255,255,255))
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype('verdana.ttf',width//numbers)
    font_width , font_height = font.getsize(strs)
    strs_len = len(strs)
    x = (width - font_width) // 2
    y = (height - font_height ) //2
    total_dex = 0
    for i in strs:
        draw.text((x,y), i, random_col(), font)
        temp = random.randint(-23,23)
        total_dex += temp
        im = im.rotate(temp)
        draw = ImageDraw.Draw(im)
        x += font_width/strs_len
    im = im.rotate(-total_dex)
    draw = ImageDraw.Draw(im)
    draw.line(
        [(random.randint(0,width//numbers),
        random.randint(0,height//numbers)
        ),
        (random.randint(width//numbers*(numbers-1),width),
        random.randint(height//numbers*(numbers-1),height)
        )],
        fill = random_col(),
        width = numbers+1)
    draw.line(
        [(random.randint(0,width//numbers),
            random.randint(height//numbers*(numbers-1),height)
        ),
        (random.randint(width//(numbers-1)*(numbers-2),width),
        random.randint(0,height//(numbers-1))
        )],
        fill = random_col(),
        width = numbers+1)
    draw.line(
        [(random.randint(width//4*3,width),
            random.randint(height//4*3,height)
        ),
        (random.randint(width//3*2,width),
        random.randint(0,height//3)
        )],
        fill = random_col(),
        width = numbers + 1)
    for x in range(width):
        for y in range(height):
            col = im.getpixel((x,y))
            if col == (255,255,255) or col == (0,0,0):
                draw.point((x,y), fill = random_col())
    im = im.filter(ImageFilter.BLUR)
    im.save('out.jpg')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号