Automate Princess Connect with Python, ADB, and OpenCV: A Step‑by‑Step Guide

This tutorial shows how to use Python, ADB, and OpenCV to automate the mobile game Princess Connect, covering environment setup, device communication, screen capture, image matching, OCR, and script snippets for clicking, typing, and switching accounts.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Automate Princess Connect with Python, ADB, and OpenCV: A Step‑by‑Step Guide

Introduction

The author, a programmer, shares a simple way to automate the mobile game Princess Connect (and similar games) by writing Python scripts that control an Android device via ADB and process screenshots with OpenCV and Tesseract OCR.

Prerequisites

Android device (emulator or real phone)

ADB installed and added to PATH

Tesseract‑OCR installed and added to PATH

Python 3.7 or newer

Links to the required ADB and Tesseract binaries are provided (e.g., Baidu Netdisk).

Python library installation pip install pillow pytesseract opencv-python Optional: uiautomator2 (not covered in this article).

Using ADB to communicate with the device

Connect to the device with adb devices, then test the shell with adb shell. If the shell hangs, restart the server using adb kill-server and retry.

Common ADB commands

Screenshot: adb shell screencap -p /data/screenshot.png then adb pull /data/screenshot.png ./tmp.png Pull file (e.g., game preferences XML):

adb pull /data/data/tw.sonet.princessconnect/shared_prefs/tw.sonet.princessconnect.v2.playerprefs.xml ./user_info.xml

Push file (switch account):

adb push ./user_info1.xml /data/data/tw.sonet.princessconnect/shared_prefs/tw.sonet.princessconnect.v2.playerprefs.xml

Tap screen: adb shell input tap X Y Input text: adb shell input text YourPassword Delete characters: repeat adb shell input keyevent 67 (backspace)

Query running package/activity: adb shell dumpsys activity activities Force‑stop app: adb shell am force-stop tw.sonet.princessconnect Start app:

adb shell am start -W -n tw.sonet.princessconnect/jp.co.cygames.activity.OverrideUnityActivity

Image operations

Use OpenCV to locate template images within screenshots. The workflow is to capture the screen, load the template, and run cv2.matchTemplate with a confidence threshold (e.g., 0.98).

import cv2

def image_to_position(screen, template):
    image_x, image_y = template.shape[:2]
    result = cv2.matchTemplate(screen, template, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
    print("prob:", max_val)
    if max_val > 0.98:
        global center
        center = (max_loc[0] + image_y / 2, max_loc[1] + image_x / 2)
        return center
    else:
        return False

if __name__ == "__main__":
    screen = cv2.imread('tmp.png')
    template = cv2.imread('Xuandan.png')
    print(image_to_position(screen, template))

The script prints a probability of 0.9977 and the position (1165, 693) on a 1280×720 screen.

Template cropping

Templates can be created by cropping screenshots with the same resolution. Using Windows 10’s Paint or QQ screenshot tool preserves pixel accuracy.

from PIL import Image

def crop_screenshot(img_file, pos_x, pos_y, width, height, out_file):
    img = Image.open(img_file)
    region = (pos_x, pos_y, pos_x + width, pos_y + height)
    cropImg = img.crop(region)
    cropImg.save(out_file)
    print("exported:", out_file)

if __name__ == "__main__":
    crop_screenshot("tmp.png", 817, 556, 190, 24, "test_id.png")

This example extracts the player ID area from the screenshot.

Simple OCR

from PIL import Image
import pytesseract

image = Image.open('test_id.png')
content = pytesseract.image_to_string(image)
print(content)

The OCR result may contain spaces and line breaks, which should be cleaned before use.

Conclusion

The article demonstrates basic ADB operations, screen capture, image matching, template cropping, and OCR to build a script that can automate account switching and other actions in Princess Connect.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonImage ProcessingOCROpenCVMobile AutomationADBGame Scripting
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.