Member-only story

CAPTCHA in Python using captcha Library

Python Coding
Aug 20, 2024

--

pip install captcha
from captcha.image import ImageCaptcha
from PIL import Image
import random

def generate_captcha_text(length=10):
return ''.join(random.choices(string.ascii_letters
+ string.digits, k=length))

def generate_captcha_image(captcha_text,image_width=300):
image = ImageCaptcha(image_width)
image_file = f"{captcha_text}.png"
image.write(captcha_text, image_file)
return image_file

captcha_text = generate_captcha_text()
image_file = generate_captcha_image(captcha_text)

print(f"Generated CAPTCHA")
Image.open(image_file)

#source Code --> clcoding.com
Generated CAPTCHA

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

No responses yet