adb截图图片有时打不开

Sometimes the screenshot image via adb can not be opend

我正在通过 adb 从我的 android 设备 (OnePlus 5) 截取一些屏幕截图,以使用 googles tesseract 识别页面内是否有指定单词。

通常需要 5-8 个屏幕截图,彼此之间有 1.5 秒的延迟。前 2-4 个屏幕截图可以打开,但随后会抛出错误,无法使用 PIL、Paint.net 或 phone 本身打开。

from adb.client import Client as AdbClient
from PIL import Image
import pytesseract
import os


# The Method with the Problem
# adb package is pure-python-adb
# after 2-4 calls of the method the error ocures
def get_screen(device):
    device.shell("screencap -p /sdcard/pgrinder/screen.png")
    device.pull("/sdcard/pgrinder/screen.png", "screen.png")
    screens = ""
    try:
        **screens = Image.open('screen.png')**
    except Exception:
       print("error with image opening")
       return -1
    return screens

当前错误是:OSError: 无法识别图像文件'C:\Users\kelle\PycharmProjects\PGrinder\tmp\screen.png'

为我修复错误的是删除此路径中的 pgrinder 目录:/sdcard/pgrinder/screen.png。

我认为如果目录不存在它会生成目录,但事实并非如此。我不知道为什么它有时有效,有时无效。但是自从我改变了它,每次都有效。