Captcha Generator

0.7.1 · active · verified Thu Apr 16

The `captcha` library (current version 0.7.1) is a Python utility designed to generate both image and audio CAPTCHAs. It provides a simple API to create various forms of captchas, useful for preventing bots in web applications. The library is actively maintained with regular releases addressing features, bug fixes, and security enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a simple image CAPTCHA. It initializes `ImageCaptcha` and then generates an image for a given text, saving it to a file. Note: Font paths may need adjustment based on your operating system. Audio captcha generation is also possible but requires additional dependencies (e.g., `pydub`, `ffmpeg`).

import os
from captcha.image import ImageCaptcha

# Generate an image captcha
image = ImageCaptcha(width=280, height=90, fonts=['/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'])
# Fallback for systems where default font path might not exist
# For Windows: C:\Windows\Fonts\arial.ttf
# For macOS: /System/Library/Fonts/Arial.ttf
# For Linux: Check common paths like /usr/share/fonts/truetype

data = image.generate('1234')

output_filename = 'captcha_image.png'
image.write('1234', output_filename)
print(f"Generated captcha image: {output_filename}")

# Generate an audio captcha (requires 'pydub' and 'ffmpeg' or 'libav' for advanced audio formats)
# from captcha.audio import AudioCaptcha
# audio = AudioCaptcha()
# audio_filename = 'captcha_audio.wav'
# audio.write('1234', audio_filename)
# print(f"Generated captcha audio: {audio_filename}")

view raw JSON →