Skip to main content

A lightweight CAPTCHA generator for Persian and English digits

Project description

Light Captcha

A lightweight Python library for generating CAPTCHA images with Persian (۰-۹) and English (0-9) digits.

Features

  • Supports Persian and English numerals
  • Random digit skewing and rotation
  • Size variations per digit
  • Millisecond-based random number generation
  • Wave distortion effects
  • Random color schemes
  • Noise and interference patterns
  • Customizable image dimensions
  • Output as PIL Image or base64 string
  • Select PNG, JPG, or animated GIF output formats
  • Reliable font loading across platforms (v0.0.3)

Installation

pip install light-captcha

Quickstart

from light_captcha import CaptchaGenerator

generator = CaptchaGenerator()

# English CAPTCHA as PIL Image (default)
result = generator.generate("english")
image = result.data
number = result.code
image.save("captcha_english.png")
print(f"Generated number: {number}")

# Persian CAPTCHA as base64 string (PNG by default)
result = generator.generate("persian")
base64_str = result.base64
number = result.code
print(f"Generated number: {number}")
print(f"Base64 data: {base64_str[:50]}...")

API Reference

CaptchaGenerator

generate(language, width=250, height=80, bg_color=None, text_color=None, type="image", format="png")

Generate a CAPTCHA image.

Parameters:

  • language (str): "english" or "persian"
  • width (int): image width in pixels (default: 250)
  • height (int): image height in pixels (default: 80)
  • bg_color (tuple, optional): RGB background color
  • text_color (tuple, optional): RGB text color
  • type (str): must be "image" (default: "image")
  • format (str): "png", "jpg", or "gif" (default: "png")

Returns:

  • CaptchaResult: object with code, data, base64, and type attributes
    • data: PIL Image
    • base64: base64 string
    • type: MIME type ("image/png", "image/jpeg", or "image/gif")

Raises:

  • ValueError: invalid language, type, or format
  • FileNotFoundError: missing font file in the package

Advanced Usage

Output Formats

result = generator.generate("english", type="image", format="png")
image = result.data
image.save("captcha.png")

result = generator.generate("english", type="image", format="jpg")
base64_str = result.base64
html_img = f'<img src="data:{result.type};base64,{base64_str}" alt="CAPTCHA">'

result = generator.generate("english", type="image", format="gif")
gif_base64 = result.base64
html_gif = f'<img src="data:{result.type};base64,{gif_base64}" alt="CAPTCHA">'

Custom Dimensions

result = generator.generate("english", width=300, height=100)
image = result.data

Custom Colors

bg_color = (240, 248, 255)
text_color = (25, 25, 112)
result = generator.generate(
    "english",
    bg_color=bg_color,
    text_color=text_color,
    type="image"
)
image = result.data

Web Integration

result = generator.generate(
    language="english",
    width=250,
    height=80,
    type="image"
)
base64_captcha = result.base64
correct_answer = result.code

# Store correct_answer in session for validation
# Send base64_captcha to frontend

Batch Generation

for i in range(5):
    result = generator.generate("persian", type="image", format="png")
    image = result.data
    image.save(f"captcha_img_{i}.png")

    result = generator.generate("english", type="image", format="jpg")
    base64_str = result.base64
    number = result.code
    with open(f"captcha_b64_{i}.txt", "w") as f:
        f.write(base64_str)

    print(f"CAPTCHA {i}: {number}")

Error Handling

try:
    generator.generate(language="invalid")
except ValueError as exc:
    print(f"Invalid language: {exc}")

Security Notes

  • Random rotation per digit: -15 to +15 degrees
  • Random scaling: 0.8x to 1.2x
  • Wave distortion with noise lines and dots
  • Multiple predefined color palettes
  • Millisecond-based seeding

Compatibility

  • Python 3.7+
  • Pillow >= 8.0.0
  • importlib_resources >= 1.3.0 (Python < 3.9)

What's New in v0.0.5

  • Renamed input to type (default: "image")
  • Output now includes data (PIL), base64, code, and MIME type
  • Added animated GIF output for image mode

What's New in v0.0.3

  • Font loading now reads font data into memory for reliable installs
  • Resource loading fallback chain improved for cross-platform consistency

License

MIT License - see LICENSE for details.

Contributing

Issues and pull requests are welcome on GitHub: https://github.com/COD332/light-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

light_captcha-0.0.5.tar.gz (116.4 kB view details)

Uploaded Source

Built Distribution

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

light_captcha-0.0.5-py3-none-any.whl (113.8 kB view details)

Uploaded Python 3

File details

Details for the file light_captcha-0.0.5.tar.gz.

File metadata

  • Download URL: light_captcha-0.0.5.tar.gz
  • Upload date:
  • Size: 116.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for light_captcha-0.0.5.tar.gz
Algorithm Hash digest
SHA256 5b84224cbc85be7ba40f5f64fe5a447ad7313149a1928b6008bea26e563bc089
MD5 185400af2282c2e2411e20589a67723b
BLAKE2b-256 186facbb5f49fdccc22b31537f1398cce26316a4365b67ae1fe22c8b51645718

See more details on using hashes here.

File details

Details for the file light_captcha-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: light_captcha-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 113.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for light_captcha-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f572d8c27c024edafd3231c80d9396dd04aaa97cc02eb5affd0c6d2fefebb1c1
MD5 a23c33cc1dd87f26a4e3630f764dfe07
BLAKE2b-256 8aa05ea7813207cdbdf565c167f5f7a70f5fdb55e6b014dac182df29bdf398a3

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