Skip to main content

Configurable CAPTCHA generator in Python with rotation, noise, and color options.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

captchagen-0.1.0.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

captchagen-0.1.0-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file captchagen-0.1.0.tar.gz.

File metadata

  • Download URL: captchagen-0.1.0.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for captchagen-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5d62b5abfb7da5be4ac5b982dddae5bbb27e2d7c0babb6e43855e7b33cf732a2
MD5 7687cb1d247c04eea7f11c89fe5a5b35
BLAKE2b-256 5c152668b9fe00cd0e5b0db4b26728016d848ce632296f31541e7dfed0472e97

See more details on using hashes here.

File details

Details for the file captchagen-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: captchagen-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for captchagen-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d5711937bc364c57c329bb4bddefa494b34073947e57a7cf6db1cc6d3daa6b3
MD5 4e691ea38eaadfd7397c2699ce9e38e6
BLAKE2b-256 aa81475f578ea34ce2f351f9cd3b37d98f9917bc592478761da471f5d8c35ec5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page