我尽量避免使用screencap -p,因为它至少比输出raw慢3倍。但我在将raw转换为png时遇到了困难。
import subprocess
import cv2
import numpy as np
output = subprocess.check_output(f'src\\adb\\adb -s {serial} shell screencap')
array = np.frombuffer(output[12:], np.uint8).reshape(720, 1280, 4)
image = cv2.cvtColor(array, cv2.COLOR_BGRA2RGB)我得到了一个例外:
Exception has occurred: ValueError
cannot reshape array of size 3691368 into shape (720,1280,4)发布于 2021-08-16 03:18:47
当我写这个问题时,我再次阅读了异常,并意识到一定是一些格式问题,例如\r\n而不是\n,导致数组大小成为那个奇怪的数字。
解决方案很简单:
output = subprocess.check_output(f'src\\adb\\adb -s {serial} shell screencap').replace(b'\r\n', b'\n')希望这对其他人有帮助。
https://stackoverflow.com/questions/68797039
复制相似问题