Pictures and text on the screen

For games and custom-drawn screens without elements, find a picture on the screenshot and tap it, or read the text with OCR. Both are optional and run on your PC.

Games, maps and some custom-drawn apps show no elements, only one big box in the dump. Coordinates work there, but they break when the layout moves. Two optional commands look at the screenshot instead:

  • find_image, tap_image, image_exists, find_images and wait_image_gone look for a small picture you cut from an earlier screenshot.
  • ocr, ocr_find and ocr_tap read the text on the screen with Tesseract.

Both run on the PC that runs droidline serve: the server takes a screenshot from the phone and does the work there, so the phone needs nothing new. Use elements whenever the dump has them; they are faster and exact.

Find a picture and tap it

  1. Take a screenshot at full size: droidline screenshot screen.png.
  2. Open it in any image editor and cut out the button or icon you want, a little tighter than its edges. Save it as PNG.
  3. Look for it:
spot = d.find_image("play-button.png")
print(spot["x"], spot["y"], spot["score"])     # center, and how close the match was

d.tap_image("play-button.png")                  # find it, then tap its center
d.tap_image("next.png", threshold=0.8, timeout=20)
const spot = await d.findImage("play-button.png");
console.log(spot.x, spot.y, spot.score);

await d.tapImage("play-button.png");
await d.tapImage("next.png", { threshold: 0.8, timeout: 20 });
var spot = await d.FindImageAsync("play-button.png");
Console.WriteLine($"{spot.X} {spot.Y} {spot.Score}");

await d.TapImageAsync("play-button.png");
await d.TapImageAsync("next.png", threshold: 0.8, timeout: 20);
droidline find_image play-button.png
droidline tap_image play-button.png
droidline tap_image next.png --threshold 0.8 --timeout 20

The SDKs also take the picture's bytes instead of a path.

Both commands try again until the picture shows up or timeout runs out (10 seconds by default), then fail with NOT_FOUND. The message gives the best score it saw, which tells you how far off it was.

What makes a match:

  • Cut it from the same phone. The picture is compared at the size it was cut, so a picture from another screen density does not match unless you pass scales (below). Keeping one set of pictures per phone model is still the most reliable.
  • Cut something distinctive. A plain colored square matches in many places; the label or the icon on it matches in one.
  • Adjust threshold with care. It runs from 0 to 1 and defaults to 0.9. Lower it a little when a button changes slightly, for example a glow or a counter. Too low and it taps something else.

From version 0.1.11 the picture commands take three options, region, scales and color, and three more commands sit next to them. Each option is off unless you pass it.

Check without waiting

image_exists answers true or false at once and never fails, which suits an if.

if d.image_exists("daily-reward.png"):
    d.tap_image("daily-reward.png")
if (await d.imageExists("daily-reward.png")) {
  await d.tapImage("daily-reward.png");
}
if (await d.ImageExistsAsync("daily-reward.png"))
    await d.TapImageAsync("daily-reward.png");
droidline image_exists daily-reward.png

Find every copy

find_images returns every place the picture appears, best match first and without overlaps, with x, y, bounds and score for each. It answers at once with an empty list when there is none, unless you give it a timeout. max caps the count, 20 by default.

for coin in d.find_images("coin.png"):
    d.tap(coin["x"], coin["y"])
for (const coin of await d.findImages("coin.png")) {
  await d.tap(coin.x, coin.y);
}
foreach (var coin in await d.FindImagesAsync("coin.png"))
    await d.TapAsync(coin.Get<int>("x"), coin.Get<int>("y"));
droidline find_images coin.png --max 50

Search part of the screen

region limits the search to a box given as left, top, right, bottom in screen pixels. It is faster, and nothing outside the box can match, so a close button in the top right corner can't be confused with one in the middle.

d.tap_image("close.png", region=[720, 0, 1440, 600])
await d.tapImage("close.png", { region: [720, 0, 1440, 600] });
await d.TapImageAsync("close.png", region: new[] { 720, 0, 1440, 600 });
droidline tap_image close.png --region 720,0,1440,600

Other screen sizes

scales tries the picture at other sizes, 1 being the size it was cut at. Use it to share one set of pictures between phones with different screen densities. Each size adds time, so list only the ones you need.

d.tap_image("play-button.png", scales=[0.75, 1, 1.25, 1.5])
await d.tapImage("play-button.png", { scales: [0.75, 1, 1.25, 1.5] });
await d.TapImageAsync("play-button.png", scales: new[] { 0.75, 1, 1.25, 1.5 });
droidline tap_image play-button.png --scales 0.75,1,1.25,1.5

Tell colors apart

The comparison looks at brightness, so a red button and a green button of the same shape can match each other. color also requires the average color to agree.

d.tap_image("green-button.png", color=True)
await d.tapImage("green-button.png", { color: true });
await d.TapImageAsync("green-button.png", color: true);
droidline tap_image green-button.png --color

Wait for a picture to go away

wait_image_gone waits until the picture is no longer on the screen, for loading spinners and transitions. It fails with TIMEOUT if the picture is still there when timeout runs out, 10 seconds by default.

d.tap_image("start.png")
d.wait_image_gone("loading.png", timeout=60)
await d.tapImage("start.png");
await d.waitImageGone("loading.png", { timeout: 60 });
await d.TapImageAsync("start.png");
await d.WaitImageGoneAsync("loading.png", timeout: 60);
droidline wait_image_gone loading.png --timeout 60

Read the text with OCR

OCR needs Tesseract on the PC that runs the server, along with the language data you read: eng for English, kor for Korean, chi_sim for simplified Chinese. Droidline does not install it. When tesseract is not on PATH, set its location in config.toml:

[ocr]
tesseract = "C:/Program Files/Tesseract-OCR/tesseract.exe"

Then:

page = d.ocr("kor+eng")
print(page["text"])                       # every line, one per row
for line in page["lines"]:
    print(line["text"], line["bounds"])

d.ocr_find("Level 12", "eng")             # wait until it shows, return where
d.ocr_tap("Start", "eng", timeout=30)     # wait, then tap it
const page = await d.ocr("kor+eng");
console.log(page.text);
for (const line of page.lines) console.log(line.text, line.bounds);

await d.ocrFind("Level 12", "eng");
await d.ocrTap("Start", "eng", { timeout: 30 });
var page = await d.OcrAsync("kor+eng");
Console.WriteLine(page.Text);
foreach (var line in page.Lines)
    Console.WriteLine($"{line.Get<string>("text")} [{string.Join(", ", line.Get<int[]>("bounds")!)}]");

await d.OcrFindAsync("Level 12", "eng");
await d.OcrTapAsync("Start", "eng", timeout: 30);
droidline ocr kor+eng
droidline ocr_find "Level 12" eng
droidline ocr_tap Start eng --timeout 30

ocr_find and ocr_tap first look for a whole word equal to your text, then for a line that contains it. Without Tesseract they fail with OCR_UNAVAILABLE, saying what is missing.

OCR reads what the picture shows, so it is slower than elements and makes mistakes with small, stylized or low-contrast text. Each try takes a screenshot and runs Tesseract once, which takes much longer than looking up an element.