图片验证码

def check_code(width=120, height=30, char_length=5, font_file="mvboli.ttf", font_size=20):
    code = []
    img = Image.new(mode="RGB", size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode="RGB")

    def rndChar():
        return chr(random.randint(65, 90))  # 生成大写字母 A-Z

    def rndColor():
        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
    # 写文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        char = rndChar()
        code.append(char)
        n = random.randint(0, 4)
        draw.text((i * width / char_length, n), char, font=font, fill=rndColor())
    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
    return img, ''.join(code)

只实现随机英文文字就行 太多了反而麻烦

你可能感兴趣的:(python)