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
- Create a virtual environment (optional but recommended):
python -m venv venv
source venv/bin/activate # Linux / macOS
venv\Scripts\activate # Windows
- Install dependencies:
pip install -r requirements.txt
Usage
-
Place your .ttf fonts inside the
fonts/directory. -
Configure your CAPTCHA settings in
generate.pyusing theCaptchaConfigclass. 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")
- 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_modeoptions:"digits"→ only 0-9"lower"→ lowercase letters"upper"→ uppercase letters"letters"→ lowercase + uppercase"alnum"→ letters + digits"custom"→ usecustom_charsetstring
- Noise is added randomly: dots and lines.
- Character rotation range: ±25 degrees.
Sample CAPTCHA
توضیحات کلی
این پروژه یک مولد کپچا قابل تنظیم در پایتون ارائه میدهد.
ویژگیها:
- پشتیبانی از چند فونت (TTF در پوشه
fonts/) - تولید متن تصادفی با مجموعه کاراکتر قابل انتخاب (
digits،letters،alnumیا دلخواه) - امکان چرخش کاراکترها
- امکان افزودن نویز (نقطه، خط یا هر دو)
- رنگ تصادفی برای کاراکترها
- تولید چند نوع کپچا در یک اسکریپت
ساختار کد به شکل کلاسبندی شده است:
CaptchaConfig: نگهدارنده تنظیمات تولید کپچاCaptchaGenerator: تولید تصویر کپچا بر اساس تنظیمات
ساختار پوشهها
project/
│
├─ fonts/ # فایلهای فونت .ttf در اینجا قرار گیرند
├─ CaptchaGen.py # کلاس تولید کپچا
├─ sample.py # اسکریپت تولید کپچا با تنظیمات مختلف
├─ requirements.txt # بستههای مورد نیاز پایتون
└─ README.md
نصب
- ایجاد محیط مجازی (اختیاری اما توصیه شده):
python -m venv venv
source venv/bin/activate # لینوکس / مک
venv\Scripts\activate # ویندوز
- نصب بستههای مورد نیاز:
pip install -r requirements.txt
نحوه استفاده
-
فونتهای
.ttfرا در پوشهfonts/قرار دهید. -
تنظیمات کپچا را در
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")
- اجرای اسکریپت تولید چند نوع کپچا:
python sample.py
- این دستور پوشه
captchas/را ایجاد کرده و تصاویر را ذخیره میکند. - نام فایلها شامل متن تولید شده و یک شماره یکتا است تا تصاویر تکراری بازنویسی نشوند.
نکات
- گزینههای
text_mode:"digits"→ فقط اعداد ۰ تا ۹"lower"→ حروف کوچک"upper"→ حروف بزرگ"letters"→ حروف کوچک + بزرگ"alnum"→ حروف + اعداد"custom"→ استفاده از رشتهcustom_charset
- نویز به صورت تصادفی: نقطه ای و خطی
- محدوده چرخش کاراکترها: ±۲۵ درجه
Sample CAPTCHA
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d62b5abfb7da5be4ac5b982dddae5bbb27e2d7c0babb6e43855e7b33cf732a2
|
|
| MD5 |
7687cb1d247c04eea7f11c89fe5a5b35
|
|
| BLAKE2b-256 |
5c152668b9fe00cd0e5b0db4b26728016d848ce632296f31541e7dfed0472e97
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d5711937bc364c57c329bb4bddefa494b34073947e57a7cf6db1cc6d3daa6b3
|
|
| MD5 |
4e691ea38eaadfd7397c2699ce9e38e6
|
|
| BLAKE2b-256 |
aa81475f578ea34ce2f351f9cd3b37d98f9917bc592478761da471f5d8c35ec5
|