Skip to content

Latest commit

 

History

History
76 lines (53 loc) · 4.88 KB

README.md

File metadata and controls

76 lines (53 loc) · 4.88 KB

Image Captcha Solver for Python (Text Captcha OCR, CapMonster Cloud)

tests License: MIT Python 3.9+ Task: ImageToText

A small Python image captcha solver: send a picture with distorted text and get the characters back, using the ImageToText task of the official CapMonster Cloud text captcha solver Python SDK. Works with files, image URLs and data: URIs, and includes a built-in demo image so the first run needs nothing but an API key.

What this example does

  1. Loads a captcha image from a local file, a URL or a base64 data URI. Without arguments it draws a demo captcha with Pillow.
  2. Checks the size limits of the API before sending anything.
  3. Sends an ImageToTextRequest with optional hints: digits only, case-sensitive answer or a math expression.
  4. Prints the recognized text.

Requirements

  • Python 3.9+
  • A CapMonster Cloud API key (see Get an API key)
  • Pillow (only for the demo image)

Quick start

git clone https://github.com/CapMonsterCloud/python-image-captcha-solver.git
cd python-image-captcha-solver
pip install -r requirements.txt

export CAPMONSTER_API_KEY=your_key        # Windows: set CAPMONSTER_API_KEY=your_key
python examples/solve_image_captcha.py                 # demo image
python examples/solve_image_captcha.py captcha.png --numeric
python examples/solve_image_captcha.py https://site.example/captcha.jpg --case

Tests (offline):

pytest -q

How it works

  • One input, three sources. load_image() accepts a path, an http(s) URL or a data:image/...;base64, string, which is how many pages embed captchas inline. For URLs, download the image in the same session that shows the form, otherwise the server may issue a new captcha.
  • Validate before you pay. check_size() rejects images under 100 bytes (usually an HTML error page saved as .png) and above 50,000 bytes, matching the API limits, so you see the problem locally.
  • Hints improve accuracy. --numeric restricts the answer to digits, --case keeps letter case, --math asks the service to calculate an expression such as 7 + 3. --module selects a specialized recognition module when one exists for the site.
  • Raw bytes in, base64 out. The SDK takes image_bytes and encodes them itself, so there is no manual base64 handling in your code.

ImageToText basics

  • The task takes a picture of the captcha and returns the recognized characters as text.
  • Send only the captcha image itself, cropped from the page if needed.
  • Each captcha is solved separately: when the site shows a new image, send a new request.

Common errors

Symptom Cause and fix
ValueError: Image is smaller than 100 bytes The URL returned an error page or a redirect. Download the image with the site's cookies.
ValueError: ... recompress it below 50000 bytes Crop the captcha element or save it as JPEG/PNG with lower quality.
[ERROR_CAPTCHA_UNSOLVABLE] The image is blank, cut off or too noisy. Send only the captcha area and check it opens in an image viewer.
The answer has the wrong case or extra spaces Use --case, or normalize the answer to the format the form expects.
PIL import error Install Pillow (pip install Pillow); it is needed only for the demo image.

Get an API key

This example needs a CapMonster Cloud account and API key. Register and get the key on the image captcha recognition page. There you will also find pricing and the API description. New users can ask support for a $0.10 test balance after registering.

Responsible use

Use this example only for captchas on websites you own or are explicitly allowed to automate, for example testing your own forms. Follow the website's terms and applicable laws, and keep your API key private.

Links