import subprocess,time,sys
from PIL import Image,ImageChops
path=sys.argv[1]; session=sys.argv[2]
def shot(p): subprocess.run(['agent-device','screenshot',p,'--session',session],check=True,stdout=subprocess.DEVNULL)
for n in range(8):
    shot(path+'.first.png'); time.sleep(.5); shot(path)
    if ImageChops.difference(Image.open(path+'.first.png').convert('RGB'),Image.open(path).convert('RGB')).getbbox() is None:
        print('Two screenshots at least 500 ms apart are pixel-identical:',path);break
else: raise SystemExit('Screenshots did not stabilize')
