Blog
How to solve image captcha with Python

How to solve image captcha with Python

Logo of Capsolver

CapSolver Blogger

How to use capsolver

21-Sep-2023

Solving image captcha, image captcha, captcha image with Python, image captcha python solver

⚙️ Prerequisites

  • Python installed
  • Capsolver API key

🤖 Step 1: Install Necessary Packages

Execute the following commands to install the required packages:

pip install capsolver

👨‍💻 Python Code for solve image captcha

Here's a Python sample script to accomplish the task:

import capsolver
import base64
from io import BytesIO

# Change to your key
capsolver.api_key = "Your Key"
## The image in base64
def to_base64(image_location, is_url=True):
    try:
        if is_url:
            response = requests.get(image_location)
            if response.status_code == 200:
                image_bytes = BytesIO(response.content)
            else:
                return "Unable to fetch image. Status code: " + str(response.status_code)
        else:
            with open(image_location, 'rb') as image_file:
                image_bytes = BytesIO(image_file.read())

        base64_string = base64.b64encode(image_bytes.read()).decode('utf-8')
        return base64_string
    except Exception as e:
        return str(e)

def solve_imagecaptcha(module, base64Image):
    solution = capsolver.solve({
        "type": "ImageToTextTask",
        "module": module,
        "body": base64Image
    })
    return solution



def main():

    imageBase64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyAPoDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+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KAPadD/wCQBpv/AF6xf+gir9FFABRRRQB//9k="
    ## Variable with the base64 image directly
    print("Solving imageCaptcha with base64 image...")
    solution = solve_imagecaptcha(module="queueit", base64Image=imageBase64 )
    print("Solution: ", solution)

    ## Variable with the image URL and convert it to base64
    # imageUrl = "https://www.google.com/recaptcha/api2/demo"
    # imageUrlBase64 = to_base64(imageUrl, is_url=True)
    # print("Solving imageCaptcha with base64 image...")
    # solution = solve_imagecaptcha(module="queueit", base64Image=imageUrlBase64 )
    # print("Solution: ", solution)

    ## Variable with the image PATH and convert it to base64
    # imagePath = "image.jpg"
    # imagePathBase64 = to_base64(imageUrl, is_url=False)
    # print("Solving imageCaptcha with base64 image...")
    # solution = solve_imagecaptcha(module="queueit", base64Image=imagePathBase64 )
    # print("Solution: ", solution)

if __name__ == "__main__":
    main()

⚠️ Change these variables

👀 More information

More