Automating Princess Connect on Android with Python, ADB, and OpenCV
This tutorial demonstrates how to use Python, ADB, and OpenCV to automate gameplay tasks in the mobile game Princess Connect, covering environment setup, device interaction, screenshot handling, image template matching, cropping, and OCR for extracting in‑game information.
The article explains how a programmer can automate the initial‑account tasks of the mobile game Princess Connect by writing Python scripts that control an Android device via ADB and perform image recognition with OpenCV, instead of using higher‑level tools like Airtest.
Preparation : You need an Android device (emulator or real phone), ADB installed and added to PATH , tesseract‑ocr for OCR, and Python 3.7+. Install required Python libraries with pip install pillow pytesseract opencv-python ; uiautomator2 is optional.
Connecting to the device : Use adb devices to list connected devices, then adb shell to verify you can enter the device shell. If the shell fails, run adb kill-server and retry.
Common ADB commands include taking screenshots, pulling and pushing files, tapping the screen, inputting text, deleting characters, querying the current package/activity, and stopping/starting apps.
Screenshot example:
def take_screenshot():
os.system("adb shell screencap -p /data/screenshot.png")
os.system("adb pull /data/screenshot.png ./tmp.png")Pulling a file (e.g., the game’s XML preferences):
os.system(f"adb pull /data/data/tw.sonet.princessconnect/shared_prefs/tw.sonet.princessconnect.v2.playerprefs.xml ./user_info.xml")Pushing a file to switch accounts:
# Switch to account 1
os.system("adb push ./user_info1.xml /data/data/tw.sonet.princessconnect/shared_prefs/tw.sonet.princessconnect.v2.playerprefs.xml")
# Switch to account 2
os.system("adb push ./user_info2.xml /data/data/tw.sonet.princessconnect/shared_prefs/tw.sonet.princessconnect.v2.playerprefs.xml")Tap a screen coordinate :
def adb_click(center, offset=(0, 0)):
(x, y) = center
x += offset[0]
y += offset[1]
os.system(f"adb shell input tap {x} {y}")Input text and delete characters :
text = "YourPassword"
os.system(f"adb shell input text {text}")
# Delete 10 characters
for i in range(10):
os.system("adb shell input keyevent 67")Query running package/activity :
adb shell dumpsys activity activitiesStop and start an app :
adb shell am force-stop tw.sonet.princessconnect
adb shell am start -W -n tw.sonet.princessconnect/jp.co.cygames.activity.OverrideUnityActivityImage template matching using OpenCV:
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 reports a matching probability of 0.9977 and a position near the bottom‑right corner of a 1280×720 screenshot.
Cropping a screenshot to obtain a template with PIL:
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’s ID area from the screenshot.
Simple OCR with pytesseract:
from PIL import Image
import pytesseract
image = Image.open('test_id.png')
content = pytesseract.image_to_string(image)
print(content)Note that the OCR result may contain spaces and line breaks that need to be filtered before use.
Conclusion : The article provides a practical guide to using ADB and basic image‑processing techniques for mobile game automation, covering device setup, command‑line interaction, screenshot handling, template matching, cropping, and OCR.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.