Skip to main content

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

Metadata

Release files for light-captcha 0.0.5

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

Source distribution (sdist)

Source distribution for light-captcha 0.0.5
File Size Uploaded Provenance Actions
light_captcha-0.0.5.tar.gz 116.4 kB Without provenance Details for light_captcha-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for light-captcha 0.0.5
File Interpreter ABI Platform Actions
light_captcha-0.0.5-py3-none-any.whl Python 3 none any Details for light_captcha-0.0.5-py3-none-any.whl

Total release size: 230.2 kB

Release files / light_captcha-0.0.5.tar.gz

Download URL light_captcha-0.0.5.tar.gz
Size 116.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5b84224cbc85be7ba40f5f64fe5a447ad7313149a1928b6008bea26e563bc089
BLAKE2b-256 checksum
How to use checksums
186facbb5f49fdccc22b31537f1398cce26316a4365b67ae1fe22c8b51645718
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / light_captcha-0.0.5-py3-none-any.whl

Download URL light_captcha-0.0.5-py3-none-any.whl
Size 113.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f572d8c27c024edafd3231c80d9396dd04aaa97cc02eb5affd0c6d2fefebb1c1
BLAKE2b-256 checksum
How to use checksums
8aa05ea7813207cdbdf565c167f5f7a70f5fdb55e6b014dac182df29bdf398a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for light-captcha 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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