18 lines
534 B
Python
18 lines
534 B
Python
from io import BytesIO
|
|
import random
|
|
import string
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
def generate_captcha():
|
|
captcha_text = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
|
|
image = Image.new('RGB', (120, 30), color = (255, 255, 255))
|
|
font = ImageFont.truetype("arial.ttf", 24)
|
|
draw = ImageDraw.Draw(image)
|
|
draw.text((10, 0), captcha_text, font=font, fill=(0, 0, 0))
|
|
|
|
buffer = BytesIO()
|
|
image.save(buffer, format="PNG")
|
|
buffer.seek(0)
|
|
|
|
return buffer, captcha_text
|