Skip to main content

Animated visual captcha generation library using Manim.

Project description

Manim-Captcha

captcha_circle captcha_matrix captcha_piramid
custom_captcha

Overview

Animated visual captcha generation library using Manim Framework.

Traditional CAPTCHAs are static images. Manim-Captcha generates animated visual challenges (e.g., moving selectors, distributed numbers, dynamic noise).

This library offers:

  • A "Captcha Generator" that allows to request generation of captcha files.
  • A "Captcha Automatic Generator" for async operation that acts as a background process to automatically generate captcha files in the filesystem for a custom interval and keeping a maximum number of captchas files (file rotation).
  • Some builtin captcha animations that you can select to use.
  • Kind of "plugin" based system were you can provide any external custom Manim captcha animation script (scene) and make the library use it.
  • Easy way to pass custom properties to the captcha scenes for customization (like scenes).

Table of Contents

Requirements

The manim-captcha library requires python 3 (tested with v3.12), Manim Community framework, and the corresponding requirements for Manim (like Cairo and Pango libraries).

Installation

Note: The next installation instructions are for Debian based Linux systems.

From a command line, install the different requirements:

sudo apt-get update
sudo apt-get install build-essential make python3 python3-dev python3-pip
sudo apt-get install libcairo2-dev libpango1.0-dev

The library has been published to pypi so can be easily installed via pip:

pip install manim-captcha

Usage

Basic captcha generator:

from manim_captcha.generator import CaptchaGenerator
from manim_captcha.scenes import CaptchaScene
from pathlib import Path

generator = CaptchaGenerator()

captcha = generator.generate(
    code="1234",
    scene=CaptchaScene.CIRCLE_NUMS,
    out_dir=Path("./captchas"),
    properties={
        "theme": "dark",
        "noise": True
    }
)

if captcha.error:
    print("Fail to create the captcha:")
    print(captcha.error_info)
else:
    print("Captcha successfully created")
    print(f"  Code: \"{captcha.code}\"")
    print(f"  File: {captcha.file}")
print("")

Async and non-blocking automatic captcha generator:

import asyncio
import logging
from manim_captcha.auto_generator import CaptchaAutoGenerator
from pathlib import Path

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)
logger = logging.getLogger(__name__)

async def main():
    # Setup the CaptchaAutoGenerator
    MAX_NUM_CAPTCHAS = 5
    TIME_GEN_INTERVAL_S = 10
    OUT_DIR = Path("./captchas")
    auto_generator = CaptchaAutoGenerator(
        OUT_DIR, TIME_GEN_INTERVAL_S, MAX_NUM_CAPTCHAS)
    # Start the generator process
    start_success = await auto_generator.start()
    if not start_success:
        logger.error("Fail to Start CaptchaAutoGenerator")
        return
    # Wait and get some captchas during 1 minute
    TIME_CHECK = 60
    time_pass_s = 0
    run = True
    while run:
        # Show number of captchas availables
        num_captchas = auto_generator.num_captchas()
        logger.info("Num Captchas Availables: %d", num_captchas)
        # Try to get a captcha
        captcha = auto_generator.get_captcha()
        if not captcha.error:
            logger.info("Captcha retrieved:")
            logger.info("  Code: \"%s\"", captcha.code)
            logger.info("  File: %s", str(captcha.file))
        await asyncio.sleep(TIME_GEN_INTERVAL_S)
        # Check if check end time has arrive to exit the loop
        time_pass_s = time_pass_s + TIME_GEN_INTERVAL_S
        if time_pass_s >= TIME_CHECK:
            run = False
    # Stop the generator process
    await auto_generator.stop()

if __name__ == '__main__':
    asyncio.run(main())

Note: You can find some extra usages in the examples/ directory.

Use my own external custom captcha

The library provides some builtin captchas, but the most useful feature is that it allows you to easily pass to the generator any external custom captcha scene that you have define.

Take a look into this example to know how it works.

Library Development

For library development, to ease project setup and installation of all the requirements, a Makefile and some bash scripts are provided for Linux systems based on Debian.

Setup the project (this will install all system requirements and creates a python virtual environment):

make setup

Extra actions can be performed through make, just run make without argments to see the usage help:

make

Usage:
  setup: Setup Project and install requirements
  test: Run project tests
  check_code_style: Run Code Style Checks
  check_static_types: Run Static Types Checks
  install: Install local package in edit mode
  publish: Publish the library to pypi

Now open any code editor (i.e. vscode) withing the python environment and start coding!

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

manim_captcha-1.2.2.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

manim_captcha-1.2.2-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file manim_captcha-1.2.2.tar.gz.

File metadata

  • Download URL: manim_captcha-1.2.2.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for manim_captcha-1.2.2.tar.gz
Algorithm Hash digest
SHA256 6616131481bbc354a9e9e206e90216a24d7d29c89f36a54a5fffff42a1e71b4d
MD5 e484f53872ff54a7aba24f729c3a3243
BLAKE2b-256 95919698724a8a7286612d4382cccf09e6d3f3b0db3f43b09a22674cbee7e248

See more details on using hashes here.

File details

Details for the file manim_captcha-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: manim_captcha-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for manim_captcha-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 736416af9240b178e3d673aa3106b337c068f8a6744c09dc13c0fbd425f684a4
MD5 2875247c58b1809fc85a9b90ae278b44
BLAKE2b-256 b0bcda55b16ff770db916f20c0582061b261f1b3ad2be4b78dd6e4369750a6d0

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