
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate CAPTCHA Using Python
A modified version of the PIL called pillow library can be used to generate a text-based CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) in Python Imaging Library.
Types of CAPTCHA
There are different types of CAPTCHA and some of them are follows below.
-
Text-based CAPTCHA : A sequence of characters is provided with features distorted and random noise to make character recognition difficult.
-
Image-based CAPTCHA : Images are provided to humans and features are distorted to make image recognition for computers difficult.
-
Audio-based CAPTCHA : An audio recording of spoken characters or clips is provided to humans which they need to input into the system correctly.
-
Behavioural CAPTCHA : To make it difficult for the bots to replicate/automate.
Generating a Text-Based CAPTCHA
By using the 'pillow' library we can generate a text-based CAPTCHA, which allows us to create and manipulate images in Python. And the steps included are as follows.
-
Creating a blank image
-
Generating Random CAPTCHA Text
-
Defining the font, text size and positioning
-
Generating and saving the CAPTCHA
Creating a Blank Image
Initializing the parameters (image width, height and length) of the image in pixels. 'Image.new('RGB', (width, height), 'white')' creates a blank image with given parameters and 'ImageDraw.Draw(image)' allows us to draw on the created image like text or noise.
def generate_captcha(width, height, length): image = Image.new('RGB', (width, height), 'white') draw = ImageDraw.Draw(image)
Generating Random Text
From the below example 'random.choices()' function generates a list of random characters and the '.join()' function joins this list of characters into a single string.
captcha_text = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
Calculating Text Size and Positioning
The 'draw.text size (captcha_text, font)' will calculate the width and height (in pixels)of the CAPTCHA text and the variables 'text_x', and 'text_y' `determines the position of the text.
font = ImageFont.truetype('arial.ttf', 72) t_width, t_height = draw.textsize(captcha_text, font) text_x = (width - t_width) / 2 text_y = (height - t_height) / 2
Generating and saving the CAPTCHA
The text used in the CAPTCHA is stored in 'captcha_text' for verification when the user submits their input.
return image, captcha_text # Define CAPTCHA parameters width = 700 # Width of the CAPTCHA image height = 350 # Height of the CAPTCHA image length = 6 # Length of the CAPTCHA text captcha_image, captcha_text = generate_captcha(width, height, length) captcha_image.save('captcha.png')
Example
from PIL import Image, ImageDraw, ImageFont import random import string def generate_captcha(width, height, length): # Create a blank image with a white background image = Image.new('RGB', (width, height), 'white') draw = ImageDraw.Draw(image) # Generate random CAPTCHA text captcha_text = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length)) # Define the font and its size font = ImageFont.truetype('arial.ttf', 72) # Calculate the text size and position it in the center t_width, t_height = draw.textsize(captcha_text, font) text_x = (width - t_width) / 2 text_y = (height - t_height) / 2 # Draw the text on the image draw.text((text_x, text_y), captcha_text, font=font, fill='black') # Add noise to the image for x in range(width): for y in range(height): if random.random() < 0.1: # Adjust noise level draw.point((x, y), fill='black') # Return the generated CAPTCHA image and the corresponding text return image, captcha_text # Define CAPTCHA parameters width = 700 height = 350 length = 6 # Generate and save the CAPTCHA image captcha_image, captcha_text = generate_captcha(width, height, length) captcha_image.save('captcha.png')