'How to get color of X Y pixel in python from android screen as fast as is possible with adb or another solution

I try write some automatic app in python, and i have to check color pixel from mobile as fast as possible. I try take screenshot, and take pixel from photo. But it is too slow,

Thanks!



Solution 1:[1]

It depends on how fast is fast for your use case. However, this is an starter idea using AndroidViewClient:

#! /usr/bin/env python3

import random
import time

from com.dtmilano.android.viewclient import ViewClient

device, serialno = ViewClient.connectToDeviceOrExit()
w = device.getProperty('display.width')
h = device.getProperty('display.height')
for n in range(20):
    x = random.randint(0, w)
    y = random.randint(0, h)
    start = time.time()
    p = device.takeSnapshot(reconnect=True).getpixel((x, y))
    print(f'{time.time() - start:.4f}: @({x:4}, {y:4}) -> {p}')

the results obtained were

0.4690: @( 795,  596) -> (245, 166, 194, 255)
0.5251: @( 330, 1580) -> (192, 102, 144, 255)
0.3421: @(  64, 1582) -> (202, 110, 152, 255)
0.3729: @( 219,  869) -> (248, 174, 201, 255)
0.3395: @( 794, 1871) -> (113, 41, 113, 255)
0.3349: @( 620,  388) -> (243, 169, 198, 255)
0.3432: @( 154,  827) -> (249, 178, 203, 255)
0.2958: @( 336,  956) -> (244, 165, 196, 255)
0.3586: @(  20, 1613) -> (200, 105, 149, 255)
0.3397: @( 119, 1692) -> (199, 89, 136, 255)
0.3915: @( 825,  942) -> (236, 151, 188, 255)
0.3343: @( 654,  306) -> (244, 170, 202, 255)
0.3416: @(  24, 1661) -> (202, 97, 144, 255)
0.4386: @( 788, 1273) -> (223, 134, 171, 255)
0.3927: @( 378,  128) -> (217, 158, 186, 255)
0.3229: @( 273, 1261) -> (235, 152, 183, 255)
0.2288: @(  19, 1567) -> (208, 116, 157, 255)
0.2286: @(   0, 1522) -> (220, 130, 164, 255)
0.3781: @( 728, 1323) -> (222, 132, 168, 255)
0.3954: @( 793, 1755) -> (154, 62, 118, 255)

There's a conversion to a PIL image that can still be removed to increase a bit the performance.

Also, using CulebraTerster2-public can speed up things a bit if previous results are not enough.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Diego Torres Milano