×

Captcha Code in Python with Example

Python Programming Language

Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said to be a user-friendly programing language, as the syntax for it is very simple to write and easy to understand for a beginner programmer. Python programming language is rich in libraries that can be imported easily and used to perform many different operations. In the year 1989, Guido van Rossum is the one who introduced python programming language.

It is also used in web applications; web applications like the Django and Flask frameworks are created using python. Compared to any programming language, the syntax in python is much easier. Many colleges and institutions have introduced python in their syllabus so that the students need to learn python. The biggest advantage of the python programming language is that it has a good collection of libraries widely used in machine learning, web frameworks, test frameworks, multimedia, image processing, and many more application. Python supports multiple programming paradigms, including object-oriented, imperative, functional, and procedural.

Captcha

Completely Automated Public Turing Test to Tell Computers and Humans Apart is referred to as CAPTCHA. It makes a distinction between humans and computers, as the name implies.The fundamental idea of CAPTCHA depends on the inability of computers to recognize patterns. Letters that have been altered or are present with a lot of background noise are not recognized by a computer. As a safety mechanism, humans have been hard-wired for aeons to recognise patterns. A visual representation of the CAPTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart).

A challenge-response authentication security method is known as CAPTCHA. CAPTCHA stops computer program from deciphering the distorted characters in the image.The security feature known as challenge-response authentication includes APTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart). By requesting that you pass a brief test to demonstrate that you are a real person and not a computer trying to access a password-protected account, CAPTCHA helps protect you from spam and password decryption.

A text box and a series of letters and/or numbers that are generated randomly and presented as distorted images make up a CAPTCHA test. There are different types of CAPTCHA available now let us observe the different kinds of CAPTCHA’S:

  • Text based image captcha:
    Text-based CAPTCHA employs a predetermined list of words or phrases as well as a random assortment of various letters, numbers, and symbols. It depicts a string of characters in an alienated form that makes them a little challenging to decipher.
    These include overlapping with neighbouring characters and distortion, rotation, flipping, wobbliness, and capitalization style. A CAPTCHA may incorporate images like lines, arcs, or dots randomly distributed throughout the image to improve this method.
  • Audio captcha:
    Visually impaired users can utilise audio CAPTCHAs as an alternative to text-based CAPTCHAs. The user must correctly enter a series of letters or numbers while listening to an audio CAPTCHA. Once more, there is distortion; but, this time, more background noise is present. It's interesting to note that the Python CAPTCHA module can only process numeric input.
  • Image captcha:
    Using an image CAPTCHA instead of one that uses text is an alternative. You will frequently have to complete CAPTCHAs like these.

    You must identify an object in these CAPTCHAs, such as a fire hydrant, truck, or boat. In addition, they provide you a selection of more photos. These tests are both much easier for humans to use and much harder for bots to understand than text-based ones.
  • Math, Word and puzzle captcha:
    Simple arithmetic problems like 14 + 22, 11 * 14, or 36 / 12 are frequently used as the basis for math CAPTCHAs.
    Word Error CAPTCHA asks you to complete a series of words or other related objects by selecting the appropriate response. Puzzle CAPTCHA asks you to complete a little jigsaw puzzle, like moving a bar to the right length, to complete a picture that is missing a piece.
  • No CAPTCHA ReCAPTCHA
    ReCAPTCHA, a bot-prevention tool owned by Google, employs cutting-edge risk analysis and adaptive challenges. ReCAPTCHA extracts information from Google Street View, publications, books, and other sources.
    Not a CAPTCHA Without requiring you to take any exams, ReCAPTCHA only asks you to select the "I'm not a robot" option. As it tracks the movements and recognises the click, this is achievable. You should be aware that ReCAPTCHA also records your personal information.

Generation of the Audio Captcha

Visually impaired users can utilize audio CAPTCHAs as an alternative to text-based CAPTCHAs. The user must correctly enter a series of letters or numbers while listening to an audio CAPTCHA. Once more, there is distortion; but, this time, more background noise is present. It's interesting to note that the Python CAPTCHA module can only process numeric input. Here we are going to generate an audio captcha:

Steps to generate the audio captcha

Step 1: Create an instance of audioCaptcha( ) using an imported module

image = audioCaptcha(width = 200, height = 60)

Step 2: audio obect generation using the objectaudio.generate(CAPTCHA_Text).

data = audio.generate(captcha_text) 

Step 3: With the help of the audio.write() we can save the file.

audio.write(captcha_text, audio_file)

Python code implementation for generation of audio captcha:

# import the required modules to perform required operation
fromcaptcha.audio importAudioCaptcha
# Creation of an instance and storing it into the audio variable.
audio =AudioCaptcha() 
# Providing the audio captcha text
captcha_text ="5252"
# Converting the given text data into the audio file
audio_data =audio.generate(captcha_text) 
# Adding name to the audio file
audio_file ="audio"+captcha_text+'.wav'
# Saving the audio file with the help of the write( )function
audio.write(captcha_text, audio_file)

Output:

We can hear the audio output through our device

Generation of the image captcha: 

Using an image CAPTCHA instead of one that uses text is an alternative. You will frequently have to complete CAPTCHAs like these.

You must identify an object in these CAPTCHAs, such as a fire hydrant, truck, or boat. In addition, they provide you a selection of more photos. These tests are both much easier for humans to use and much harder for bots to understand than text-based ones

Here we are going to generate an image captcha:

Implementation using the Python Code

Step 1: ImageCaptcha() instance generation with the help of imported module

image = ImageCaptcha(width = 250, height = 60)

Step 2: With the help of  image.generate(CAPTCHA_Text) create an object

data = image.generate(captcha_text) 

Step 3: With the help of image.write() save the file

image.write(captcha_text, 'CAPTCHA.png')

Implementation

# Import the required modules to perform required operationn
fromcaptcha.image importImageCaptcha
# Creation of an instance and storing it into the image variable.
image =ImageCaptcha(width =250, height =60)
# Image captcha text
captcha_text ='Python' 
# Converting the given text data into the image file
data =image.generate(captcha_text)  
# Saving the image file with the help of the write( )function
image.write(captcha_text, 'CAPTCHA.png')

Output:

PYTHON

Image CAPTCHA

Conclusion

Completely Automated Public Turing Test to Tell Computers and Humans Apart is referred to as CAPTCHA. It makes a distinction between humans and computers, as the name implies. The fundamental idea of CAPTCHA depends on the inability of computers to recognise patterns. Letters that have been altered or are present with a lot of background noise are not recognised by a computer. As a safety mechanism, humans have been hard-wired for aeons to recognise patterns.

A visual representation of the CAPTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart). A challenge-response authentication security method is known as CAPTCHA. CAPTCHA stops computer programmes from deciphering the distorted characters in the image. The security feature known as challenge-response authentication includes APTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart).

By requesting that you pass a brief test to demonstrate that you are a real person and not a computer trying to access a password-protected account, CAPTCHA helps protect you from spam and password decryption.Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said to be a user-friendly programing language, as the syntax for it is very simple to write and easy to understand for a beginner programmer. Python programming language is rich in libraries that can be imported easily and used to perform many different operations


Related Topics

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python program to check whether a given number is prime or not

Python program to check whether a given number is prime or not A positive integer greater than 1 is called a prime number if it is divisible by one and number...

1 minute read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

Best Way to Learn Python for Free

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. Learning Python is a step towards coding. Python gets...

5 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.