使用Python屏幕截图ADB

发布于 2025-01-30 09:03:33 字数 709 浏览 1 评论 0原文

因此,我试图截图ADB并纯粹是从Python截然不同的,因此用户设备上不会保存任何文件。

我创建了以下辅助功能以进行操作,

def adb_run(command, verbose=False):
    result = subprocess.run([r"D:\Program Files\Nox\bin\adb.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解决方案

so I am trying to screenshot adb and handle it purely from Python hence there will be no files saved on the users device.

I created the following helper functions in order to do so

def adb_run(command, verbose=False):
    result = subprocess.run([r"D:\Program Files\Nox\bin\adb.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")

I then called the screencap() function and wrote it to a file, the content seems to be a valid PNG file, but I am not able to load the png file since it says it's corrupted

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

Does anyone know why the image file would be corrupted? I haven't found any pure python solutions online

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2025-02-06 09:03:33

尝试使用exec-out而不是shell

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

Try using exec-out rather than shell.

def screencap(filename):
    return adb_run(['exec-out', f'screencap -p > {filename}'])
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文