Files
website_design/Backend/captcha.py
T

29 lines
771 B
Python

from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import random
import string
def generate_captcha():
# Create an image with white background
image = Image.new('RGB', (100, 40), (255, 255, 255))
draw = ImageDraw.Draw(image)
# Choose a font and size
font = ImageFont.truetype("arial.ttf", 24)
# Generate random captcha text
text = ''.join(random.choices(string.ascii_letters + string.digits, k=5))
# Draw the text on the image
draw.text((10, 5), text, font=font, fill=(0, 0, 0))
# Distort the image (optional)
# Add more distortion if needed
# Save the image to a BytesIO object
output = BytesIO()
image.save(output, format='PNG')
output.seek(0)
return output