Generated CAPTCHA in Python using captcha Library

mahabub.devs3
Mahabubur Rahman
Published on Oct, 20 2024 1 min read 0 comments
image

Install Package:

 

pip install captcha

 

Using:

 


import string

from PIL import Image
import random

from captcha.image import ImageCaptcha



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)

 

 

 

 

0 Comments