Skip to main content

CAPTCHA Generator

Overview

This project provides a configurable CAPTCHA generator in Python.
It supports:

  • Multiple font support (TTF files in fonts/ folder)
  • Random text generation with configurable character sets (digits, letters, alnum, or custom)
  • Optional character rotation
  • Optional noise (dots, lines, or both)
  • Random character colors
  • Multi-configuration generation in one script

The code is organized into a class-based structure:

  • CaptchaConfig: Holds configuration parameters for CAPTCHA generation.
  • CaptchaGenerator: Generates CAPTCHA images based on the configuration.

Folder Structure

project/
│
├─ fonts/           # Place your .ttf font files here
├─ CaptchaGen.py # CAPTCHA generator class
├─ sample.py      # Script to generate CAPTCHA images with multiple configurations
├─ requirements.txt # Required Python packages
└─ README.md

Installation

  1. Create a virtual environment (optional but recommended):
python -m venv venv
source venv/bin/activate  # Linux / macOS
venv\Scripts\activate     # Windows
  1. Install dependencies:
pip install -r requirements.txt

Usage

  1. Place your .ttf fonts inside the fonts/ directory.

  2. Configure your CAPTCHA settings in generate.py using the CaptchaConfig class. Example:

from CaptchaGen import CaptchaGenerator, CaptchaConfig

cfg = CaptchaConfig(
    noise=True,
    rotate=True,
    random_color=True,
    text_mode="alnum"
)

gen = CaptchaGenerator("fonts", cfg)
img, label = gen.generate()
img.save(f"captchas/{label}.png")
  1. Run the multi-configuration generation script:
python sample.py
  • This will create captchas/ folder with images for all configurations.
  • Filenames include both the generated text and a unique index to avoid overwriting duplicates.

Notes

  • text_mode options:
    • "digits" → only 0-9
    • "lower" → lowercase letters
    • "upper" → uppercase letters
    • "letters" → lowercase + uppercase
    • "alnum" → letters + digits
    • "custom" → use custom_charset string
  • Noise is added randomly: dots and lines.
  • Character rotation range: ±25 degrees.

Sample CAPTCHA

Sample CAPTCHA


توضیحات کلی

این پروژه یک مولد کپچا قابل تنظیم در پایتون ارائه می‌دهد.
ویژگی‌ها:

  • پشتیبانی از چند فونت (TTF در پوشه fonts/)
  • تولید متن تصادفی با مجموعه کاراکتر قابل انتخاب (digits، letters، alnum یا دلخواه)
  • امکان چرخش کاراکترها
  • امکان افزودن نویز (نقطه، خط یا هر دو)
  • رنگ تصادفی برای کاراکترها
  • تولید چند نوع کپچا در یک اسکریپت

ساختار کد به شکل کلاس‌بندی شده است:

  • CaptchaConfig: نگه‌دارنده تنظیمات تولید کپچا
  • CaptchaGenerator: تولید تصویر کپچا بر اساس تنظیمات

ساختار پوشه‌ها

project/
│
├─ fonts/           # فایل‌های فونت .ttf در اینجا قرار گیرند
├─ CaptchaGen.py # کلاس تولید کپچا
├─ sample.py      # اسکریپت تولید کپچا با تنظیمات مختلف
├─ requirements.txt # بسته‌های مورد نیاز پایتون
└─ README.md

نصب

  1. ایجاد محیط مجازی (اختیاری اما توصیه شده):
python -m venv venv
source venv/bin/activate  # لینوکس / مک
venv\Scripts\activate     # ویندوز
  1. نصب بسته‌های مورد نیاز:
pip install -r requirements.txt

نحوه استفاده

  1. فونت‌های .ttf را در پوشه fonts/ قرار دهید.

  2. تنظیمات کپچا را در generate.py با استفاده از کلاس CaptchaConfig مشخص کنید:

from CaptchaGen import CaptchaGenerator, CaptchaConfig

cfg = CaptchaConfig(
    noise=True,
    rotate=True,
    random_color=True,
    text_mode="alnum"
)

gen = CaptchaGenerator("fonts", cfg)
img, label = gen.generate()
img.save(f"captchas/{label}.png")
  1. اجرای اسکریپت تولید چند نوع کپچا:
python sample.py
  • این دستور پوشه captchas/ را ایجاد کرده و تصاویر را ذخیره می‌کند.
  • نام فایل‌ها شامل متن تولید شده و یک شماره یکتا است تا تصاویر تکراری بازنویسی نشوند.

نکات

  • گزینه‌های text_mode:
    • "digits" → فقط اعداد ۰ تا ۹
    • "lower" → حروف کوچک
    • "upper" → حروف بزرگ
    • "letters" → حروف کوچک + بزرگ
    • "alnum" → حروف + اعداد
    • "custom" → استفاده از رشته custom_charset
  • نویز به صورت تصادفی: نقطه ای و خطی
  • محدوده چرخش کاراکترها: ±۲۵ درجه

Sample CAPTCHA

Sample CAPTCHA

Metadata

Release files for CaptchaGen 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for CaptchaGen 0.1.0
File Size Uploaded
captchagen-0.1.0.tar.gz 21.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for CaptchaGen 0.1.0
File Interpreter ABI Platform
captchagen-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 39.5 kB

Release files / captchagen-0.1.0.tar.gz

Download URL captchagen-0.1.0.tar.gz
Size 21.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5d62b5abfb7da5be4ac5b982dddae5bbb27e2d7c0babb6e43855e7b33cf732a2
BLAKE2b-256 checksum
How to use checksums
5c152668b9fe00cd0e5b0db4b26728016d848ce632296f31541e7dfed0472e97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.8

Release files / captchagen-0.1.0-py3-none-any.whl

Download URL captchagen-0.1.0-py3-none-any.whl
Size 18.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d5711937bc364c57c329bb4bddefa494b34073947e57a7cf6db1cc6d3daa6b3
BLAKE2b-256 checksum
How to use checksums
aa81475f578ea34ce2f351f9cd3b37d98f9917bc592478761da471f5d8c35ec5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page