How to Solve Image captcha with Python

Ethan Collins
Pattern Recognition Specialist
21-Sep-2023


Introduction
Image-to-Text captchas are one of the most common forms of bot verification on the internet. They present an image containing distorted, warped, or obscured characters and require the user to correctly identify and enter the text to prove they are human. For developers working on web scraping or automation, these captchas are a widespread obstacle.
Fortunately, with AI-powered recognition services, this process can be easily automated. This guide will show you how to use Python and the CapSolver service to solve image captchas quickly and accurately.
⚙️ Prerequisites
Before you begin, make sure you have the following:
- Python installed: Ensure Python is set up on your system.
- CapSolver API Key: You will need a CapSolver account to get your API key from the dashboard.
🤖 Step 1: Install Necessary Python Packages
First, you need to install the official CapSolver Python library. This package simplifies interaction with the CapSolver API. Open your terminal and run the following command:
bash
pip install capsolver
👨💻 Step 2: Write Python Code to Solve Image Captcha
The following Python script demonstrates how to solve an image captcha using CapSolver. The core of this code is the ImageToTextTask, which leverages CapSolver's powerful Vision Engine to recognize the text within an image.
Updated Python Code
This code is more modular and includes functions to handle both local image files and image URLs, making it more versatile for different use cases.
python
import capsolver
import base64
import requests
from io import BytesIO
# -------------------PLEASE MODIFY THESE VALUES-------------------
# Your API key from the CapSolver Dashboard
CAPSOLVER_API_KEY = "Your_API_KEY"
# ----------------------------------------------------------------
def setup_capsolver():
"""Sets the CapSolver API key."""
capsolver.api_key = CAPSOLVER_API_KEY
def get_base64_from_image(source, is_url=False):
"""
Converts an image file or URL into a Base64 encoded string.
:param source: The path (for a local file) or URL of the image.
:param is_url: True if the source is a URL.
:return: A Base64 encoded string, or None on failure.
"""
try:
if is_url:
response = requests.get(source)
response.raise_for_status() # Raise an exception for bad status codes
image_bytes = BytesIO(response.content)
else:
with open(source, 'rb') as image_file:
image_bytes = BytesIO(image_file.read())
base64_string = base64.b64encode(image_bytes.read()).decode('utf-8')
return base64_string
except requests.exceptions.RequestException as e:
print(f"Could not fetch image from URL: {e}")
return None
except FileNotFoundError:
print(f"Error: File not found at {source}")
return None
except Exception as e:
print(f"An error occurred converting image to Base64: {e}")
return None
def solve_image_captcha(base64_image, module_name="common"):
"""
Solves an image captcha using CapSolver.
:param base64_image: The Base64 encoded image string.
:param module_name: The recognition module, 'common' for general purpose.
:return: The solution object, or None on failure.
"""
print("Submitting image captcha task to CapSolver...")
try:
solution = capsolver.solve({
"type": "ImageToTextTask",
"module": module_name,
"body": base64_image
})
return solution
except Exception as e:
print(f"An error occurred while solving the captcha: {e}")
return None
def main():
"""Main function to execute the process."""
setup_capsolver()
# --- Example 1: Using a local image file ---
print("--- Example 1: Using a local image file ---")
local_image_path = "path/to/your/captcha.jpg" # Replace with your image path
base64_from_file = get_base64_from_image(local_image_path)
if base64_from_file:
solution = solve_image_captcha(base64_from_file)
if solution:
print("Solution Text:", solution.get("text"))
print("\n" + "="*30 + "\n")
# --- Example 2: Using an image URL ---
print("--- Example 2: Using an image URL ---")
image_url = "https://i.postimg.cc/B6hK2V19/captcha-example.png" # Example image URL
base64_from_url = get_base64_from_image(image_url, is_url=True)
if base64_from_url:
solution = solve_image_captcha(base64_from_url)
if solution:
print("Solution Text:", solution.get("text"))
if __name__ == "__main__":
main()
⚠️ Important Variables to Change
Before running the code, be sure to modify the following:
CAPSOLVER_API_KEY: Find your API key in the CapSolver Dashboard and replace the placeholder.local_image_path(in themainfunction): If you want to test a local image, replace this with the actual file path to your captcha image.
Conclusion
By integrating CapSolver into your Python scripts, you can fully automate the process of recognizing image captchas. This method is not only efficient and accurate but also frees you from the repetitive manual labor of solving these challenges. It's a critical efficiency boost for any project involving large-scale data collection or automated testing. If you're interested in tackling more complex captcha types, you can learn how to solve reCAPTCHA v2 with Playwright, which demonstrates handling dynamic challenges in a browser automation context.
Frequently Asked Questions (FAQ)
Q1: How accurate is CapSolver's recognition?
A1: CapSolver uses advanced AI models and achieves a high accuracy rate for most standard image captchas. The accuracy can vary depending on the complexity of the captcha (e.g., level of distortion, background noise).
Q2: What is the module parameter for?
A2: The module parameter allows you to specify a particular recognition engine to optimize performance for a certain type of captcha. common is the general-purpose module suitable for most cases. For more specialized modules, refer to the official ImageToTextTask documentation.
Q3: Can I use this method directly in my web scraper?
A3: Absolutely. Once you have the recognized text, you can use it to fill out the captcha form and proceed with your scraping or automation workflow. This method can be seamlessly integrated into any Python project.
Q4: Does CapSolver support languages other than Python?
A4: Yes. CapSolver provides libraries and API endpoints for multiple programming languages, including Node.js, Go, and others, making it easy to integrate into virtually any tech stack.
Compliance Disclaimer: The information provided on this blog is for informational purposes only. CapSolver is committed to compliance with all applicable laws and regulations. The use of the CapSolver network for illegal, fraudulent, or abusive activities is strictly prohibited and will be investigated. Our captcha-solving solutions enhance user experience while ensuring 100% compliance in helping solve captcha difficulties during public data crawling. We encourage responsible use of our services. For more information, please visit our Terms of Service and Privacy Policy.
More

Web scraping with Cheerio and Node.js 2026
Web scraping with Cheerio and Node.js in 2026 remains a powerful technique for data extraction. This guide covers setting up the project, using Cheerio's Selector API, writing and running the script, and handling challenges like CAPTCHAs and dynamic pages.

Ethan Collins
20-Nov-2025

Best Captcha Solving Service 2026, Which CAPTCHA Service Is Best?
Compare the best CAPTCHA solving services for 2026. Discover CapSolver's cutting-edge AI advantage in speed, 99%+ accuracy, and compatibility with Captcha Challenge

Lucas Mitchell
30-Oct-2025

Web Scraping vs API: Collect data with web scraping and API
Learn the differences between web scraping and APIs, their pros and cons, and which method is best for collecting structured or unstructured web data efficiently.

Rajinder Singh
29-Oct-2025

Auto-Solving CAPTCHAs with Browser Extensions: A Step-by-Step Guide
Browser extensions have revolutionized the way we interact with websites, and one of their remarkable capabilities is the ability to auto-solve CAPTCHAs..

Ethan Collins
23-Oct-2025

Solving AWS WAF Bot Protection: Advanced Strategies and CapSolver Integration
Discover advanced strategies for AWS WAF bot protection, including custom rules and CapSolver integration for seamless CAPTCHA solution in compliant business scenarios. Safeguard your web applications effectively.

Lucas Mitchell
23-Sep-2025

What is AWS WAF: A Python Web Scraper's Guide to Seamless Data Extraction
Learn how to effectively solve AWS WAF challenges in web scraping using Python and CapSolver. This comprehensive guide covers token-based and recognition-based solutions, advanced strategies, and code examples fo easy data extraction.

Lucas Mitchell
19-Sep-2025

