captcha: A Python Library for Audio and Image CAPTCHAs

captcha: A Python Library for Audio and Image CAPTCHAs

Summary

captcha is a Python library designed to generate both audio and image CAPTCHAs. It offers a straightforward way to integrate robust human verification into web applications, enhancing security and user experience. With customizable options for fonts, voices, and colors, it provides flexibility for various project needs.

Repository Info

Updated on November 5, 2025
View on GitHub

Introduction

The captcha library by lepture is a powerful Python tool for generating both audio and image CAPTCHAs. It provides a simple yet effective solution for implementing human verification challenges, crucial for preventing spam and automated abuse on websites and applications. This library is written in Python and is widely used for its ease of integration and flexibility.

Installation

Installing captcha is straightforward using pip. Open your terminal or command prompt and run the following command:

pip install captcha

This will install the library and its dependencies, allowing you to start generating CAPTCHAs immediately.

Examples

The library separates audio and image CAPTCHA generation into distinct modules, making it easy to use. Here are examples demonstrating how to generate both types of CAPTCHAs and how to customize image colors.

Basic Usage:

from captcha.audio import AudioCaptcha
from captcha.image import ImageCaptcha

audio = AudioCaptcha(voicedir='/path/to/voices') # Optional: use your own voice data
image = ImageCaptcha(fonts=['/path/A.ttf', '/path/B.ttf']) # Optional: use your own font data

# Generate audio CAPTCHA
data_audio = audio.generate('1234')
audio.write('1234', 'out.wav')

# Generate image CAPTCHA
data_image = image.generate('1234')
image.write('1234', 'out.png')

While the library includes built-in voice and font data, it is recommended to use your own custom data for production environments.

Custom Colors for Image CAPTCHA:

You can easily customize the background and foreground colors of your image CAPTCHAs by specifying RGB tuples.

from captcha.image import ImageCaptcha

image = ImageCaptcha(fonts=['/path/A.ttf', '/path/B.ttf'])

# Red text on a yellow background
image.write('1234', 'out.png', bg_color=(255, 255, 0), fg_color=(255, 0, 0))

Why Use

The captcha library stands out for its simplicity and effectiveness in generating both visual and auditory challenges. Its Pythonic interface makes it easy for developers to integrate robust CAPTCHA functionality into their projects. Whether you need to protect forms, prevent bot registrations, or secure sensitive actions, captcha offers a reliable and customizable solution. The ability to use custom fonts and voices also allows for greater brand consistency and accessibility.

Links

For more detailed information, documentation, and to contribute to the project, refer to the official links: