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.
- Loads a captcha image from a local file, a URL or a base64 data URI. Without arguments it draws a demo captcha with Pillow.
- Checks the size limits of the API before sending anything.
- Sends an
ImageToTextRequestwith optional hints: digits only, case-sensitive answer or a math expression. - Prints the recognized text.
- Python 3.9+
- A CapMonster Cloud API key (see Get an API key)
- Pillow (only for the demo image)
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 --caseTests (offline):
pytest -q- One input, three sources.
load_image()accepts a path, anhttp(s)URL or adata: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.
--numericrestricts the answer to digits,--casekeeps letter case,--mathasks the service to calculate an expression such as7 + 3.--moduleselects a specialized recognition module when one exists for the site. - Raw bytes in, base64 out. The SDK takes
image_bytesand encodes them itself, so there is no manual base64 handling in your code.
- 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.
| 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. |
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.
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.
- Python SDK: CapMonster Cloud Python SDK
- Image captcha recognition with CapMonster Cloud: capmonster.cloud/en/image-to-text