from pathlib import Path
from PIL import Image,ImageChops
import os,subprocess,time,sys,tempfile,shutil
root=Path('/tmp/factory-issue-36296-evidence-3')
side,label=sys.argv[1:]
work=Path(tempfile.mkdtemp(prefix='capture-',dir=root))
env={**os.environ,'AGENT_DEVICE_STATE_DIR':str(root/'agent-device-state')}
def screenshot(path):
 subprocess.run(['agent-device','screenshot',str(path),'--session','issue-36296-android'],env=env,check=True,capture_output=True)
for n in range(12):
 a=work/f'{n}-a.png';b=work/f'{n}-b.png';screenshot(a);time.sleep(.5);screenshot(b)
 if ImageChops.difference(Image.open(a).convert('RGB'),Image.open(b).convert('RGB')).getbbox() is None:
  dest=root/f'android-{side}-{label}.png';shutil.copyfile(b,dest);print(dest);break
else:raise SystemExit('Capture did not settle')
