Python to Encode CAPTCHA with Source Code

In this article, you’ll learn how to encode CAPTCHA using python.

Prerequisites:

  1. Python Basics
  2. CAPTCHA Basics
  3. CAPTCHA module

What is CAPTCHA?

AcronymStands for
CCompletely
AAutomated
PPublic
TTuring test to tell
CComputers and
HHumans
AApart
  • Its primary motive is to determine whether the user is a real human or a spam robot.
  • CAPTCHA is an example of one-way conversion and a type of challenge-response test used in computing to determine whether or not the user is human.
  • The most common form is Image CAPTCHA.
  • You are shown an image and if you are a real person, then you need to enter its text in a separate field.

Types of CAPTCHA

1. Image CAPTCHA

The CAPTCHA presents characters in a way that is alienated and requires interpretation. Alienation can involve scaling, rotation, distorting characters. It can also involve overlapping characters with graphic elements such as colour, background noise, lines, arcs, or dots. This alienation provides protection against bots with insufficient text recognition algorithms but can also be difficult for humans to interpret.

2. Audio CAPTCHA

  • Audio CAPTCHAs were developed as an alternative that grants accessibility to visually impaired users.
  • These CAPTCHAs are often used in combination with text or image-based CAPTCHAs.
  • Audio CAPTCHAs present an audio recording of a series of letters or numbers which a user then enters.
  • These CAPTCHAs rely on bots not being able to distinguish relevant characters from background noise.
  • Like text-based CAPTCHAs, these tools can be difficult for humans to interpret as well as for bots.

Install Necessary Modules:

Open your Anaconda Prompt and type and run the following command (individually):

pip install captcha

Once Installed now we can import it inside our python code.

Source Code:

CAPTCHA is used by any website that wishes to restrict usage by bots.

1. Image CAPTCHA

'''
Python Program to Encode and Decode Text CAPTCHA
'''

# Import the necessary module!
from captcha.image import ImageCaptcha

# Store and define dimentions
image = ImageCaptcha(width = 500, height = 100)

# Define data
data = image.generate("9Pyth0n4d@tA6")

# Write to the file
image.write("9Pyth0n4d@ta6","CAPTCHA_1.png")

Output:

9Pyth0n4d@ta6Code language: CSS (css)

2. Audio CAPTCHA

'''
Python Program to Encode Audio CAPTCHA
'''

# Import the necessary module!
from captcha.audio import AudioCaptcha

# Define properties
audio = AudioCaptcha()

# Define data
data = audio.generate("978")

# Write to the file
audio.write("978", "CAPTCHA_2.wav")

Output:

59840

Leave a Comment