Generate Captcha Using Python
Last Updated :
08 Oct, 2021
In this article, we are going to see how to generate a captcha using Python package captcha to generate our own CAPTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart) in picture form. CAPTCHA is a form of challenge-response authentication security mechanism. CAPTCHA prevents automated systems from reading the distorted characters in the picture.
Installation:
pip install captcha
Generating image captcha:
Here we are going to generate an image captcha:
Stepwise implementation:
Step 1: Import module and create an instance of ImageCaptcha().
image = ImageCaptcha(width = 280, height = 90)
Step 2: Create image object with image.generate(CAPTCHA_Text).
data = image.generate(captcha_text)
Step 3: Save the image to a file image.write().
image.write(captcha_text, 'CAPTCHA.png')
Below is the full implementation:
Python3
from captcha.image import ImageCaptcha
image = ImageCaptcha(width = 280 , height = 90 )
captcha_text = 'GeeksforGeeks'
data = image.generate(captcha_text)
image.write(captcha_text, 'CAPTCHA.png' )
|
Output:
Image CAPTCHA
Generating Audio captcha:
Here we are going to generate an audio captcha:
Stepwise implementation:
Step 1: Import module and create an instance of AudioCaptcha().
image = audioCaptcha(width = 280, height = 90)
Step 2: Create an audio object with audio.generate(CAPTCHA_Text).
data = audio.generate(captcha_text)
Step 3: Save the image to file audio.write().
audio.write(captcha_text, audio_file)
Below is the full implementation:
Python3
from captcha.audio import AudioCaptcha
audio = AudioCaptcha()
captcha_text = "5454"
audio_data = audio.generate(captcha_text)
audio_file = "audio" + captcha_text + '.wav'
audio.write(captcha_text, audio_file)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...