Skip to content

Logic CAPTCHAs

Sepehr0Day edited this page Jun 13, 2026 · 2 revisions

Logic CAPTCHAs

MathCaptcha

Returns a plain expression for applications that render their own UI.

from CaptchaGenerator import MathCaptcha

expression, expected = MathCaptcha().generate()
print("Question:", expression)

user_answer = "12"
try:
    solved = int(user_answer) == expected
except ValueError:
    solved = False

print("Solved:", solved)

MathImageCaptcha

Renders the arithmetic expression into an image.

from CaptchaGenerator import CaptchaConfig, MathImageCaptcha

result = MathImageCaptcha(
    CaptchaConfig(width=720, height=240, random_seed=42)
).generate(
    name_export="math_image",
    path_export="./output",
    fonts=[],
    difficulty="hard",
    style="wave",
)

print("Show image:", result.path)
print("Instruction:", result.prompt)

user_answer = "12"
solved = user_answer.strip() == str(result.answer)
print("Solved:", solved)

SequenceCaptcha

Creates a readable arithmetic or geometric sequence.

from CaptchaGenerator import CaptchaConfig, SequenceCaptcha

result = SequenceCaptcha(
    CaptchaConfig(width=900, height=260, random_seed=42)
).generate(
    name_export="sequence",
    path_export="./output",
    fonts=[],
    difficulty="hard",
    style="minimal",
)

print("Show image:", result.path)
print("Instruction:", result.prompt)

user_answer = "729"
solved = user_answer.strip() == str(result.answer)
print("Solved:", solved)

# Keep these fields server-side because they reveal the rule:
print(result.metadata["sequence"])
print(result.metadata["rule"])
print(result.metadata["rule_value"])

LogicChallengeCaptcha

Produces a compact visual logic question.

from CaptchaGenerator import CaptchaConfig, LogicChallengeCaptcha

result = LogicChallengeCaptcha(
    CaptchaConfig(width=900, height=300, random_seed=42)
).generate(
    name_export="logic",
    path_export="./output",
    fonts=[],
    difficulty="medium",
)

print("Show image:", result.path)
print("Instruction:", result.prompt)

user_answer = "true"
solved = user_answer.strip().casefold() == str(result.answer).casefold()
print("Solved:", solved)

CustomQuestionCaptcha

Uses application-defined questions and answers.

from CaptchaGenerator import CaptchaConfig, CustomQuestionCaptcha

questions = [
    ("What is the capital of France?", "Paris"),
    ("How many days are in a week?", 7),
    ("Type the word BLUE", "BLUE"),
]

result = CustomQuestionCaptcha(
    CaptchaConfig(width=900, height=360, random_seed=42)
).generate(
    name_export="custom_question",
    path_export="./output",
    questions=questions,
)

print("Show image:", result.path)
print("Question:", result.prompt)

user_answer = "Paris"
solved = user_answer.strip().casefold() == str(result.answer).casefold()
print("Solved:", solved)

A dynamic provider is also supported:

def provide_question() -> tuple[str, object]:
    return "What is 5 + 7?", 12


result = CustomQuestionCaptcha().generate(
    name_export="dynamic",
    path_export="./output",
    provider=provide_question,
)

Clone this wiki locally