使用Python截图ADB



所以我试图截图adb和处理它纯粹从Python因此不会有文件保存在用户的设备上。

为了做到这一点,我创建了以下帮助函数
def adb_run(command, verbose=False):
result = subprocess.run([r"D:Program FilesNoxbinadb.exe"] + command, stdout=subprocess.PIPE, errors="ignore")
if verbose:
print(result.stdout)
return result

def shell(command, verbose=False):
return adb_run(['shell', command], verbose)

def screencap():
return shell(f"screencap -p")

然后我调用screencap()函数并将其写入文件,内容似乎是一个有效的PNG文件,但我无法加载PNG文件,因为它说它已损坏

capped = screencap().stdout
with open("screencap.png", "wb") as f:
f.write(capped.encode())

有谁知道为什么图像文件会损坏?我没有在网上找到任何纯python的解决方案

尝试使用exec-out而不是shell

def screencap(filename):
return adb_run(['exec-out', f'screencap -p > {filename}'])

最新更新