我希望在没有adb screencap
标志的情况下使用-p
实用程序。我想象输出将以原始格式转储,但看起来不像。我尝试使用Pillow
(python)库打开原始图像文件导致:
$ adb pull /sdcard/screenshot.raw screenshot.raw
$ python
>>> from PIL import Image
>>> Image.open('screenshot.raw')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/....../lib/python2.7/site-packages/PIL/Image.py", line 2025, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
找不到像这样阅读原始图像的正确方法,我甚至给了以下镜头:How to read a raw image using PIL?
>>> with open('screenshot.raw', 'rb') as f:
... d = f.read()
...
>>> from PIL import Image
>>> Image.frombuffer('RGB', len(d), d)
__main__:1: RuntimeWarning: the frombuffer defaults may change in a future release; for portability, change the call to read:
frombuffer(mode, size, data, 'raw', mode, 0, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/..../lib/python2.7/site-packages/PIL/Image.py", line 1896, in frombuffer
return frombytes(mode, size, data, decoder_name, args)
File "/Users/..../lib/python2.7/site-packages/PIL/Image.py", line 1821, in frombytes
im = new(mode, size)
File "/Users/..../lib/python2.7/site-packages/PIL/Image.py", line 1787, in new
return Image()._new(core.fill(mode, size, color))
TypeError: must be 2-item sequence, not int
所有可能的模式选项都导致相同的TypeError
例外。
以下是hexdump
实用程序显示的内容:
$ hexdump -C img.raw | head
00000000 d0 02 00 00 00 05 00 00 01 00 00 00 1e 1e 1e ff |................|
00000010 1e 1e 1e ff 1e 1e 1e ff 1e 1e 1e ff 1e 1e 1e ff |................|
*
000038c0 1e 1e 1e ff 1e 1e 1e ff 21 21 21 ff 2b 2b 2b ff |........!!!.+++.|
000038d0 1e 1e 1e ff 1e 1e 1e ff 1e 1e 1e ff 1e 1e 1e ff |................|
*
00004400 1e 1e 1e ff 1e 1e 1e ff 47 47 47 ff 65 65 65 ff |........GGG.eee.|
00004410 20 20 20 ff 1e 1e 1e ff 1e 1e 1e ff 1e 1e 1e ff | .............|
00004420 1e 1e 1e ff 1e 1e 1e ff 1e 1e 1e ff 1e 1e 1e ff |................|
*
在osx上:
$ file screenshot.raw
screenshot.raw: data
screencap
帮助页面对于没有-p
标志的输出数据格式没有太多了解:
$ adb shell screencap -h
usage: screencap [-hp] [FILENAME]
-h: this message
-p: save the file as a png.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.
答案 0 :(得分:10)
格式:
width
height
pixel format
width
* heigth
* bytespp
)字节为字节数组 - image data
,其中bytespp
为每像素字节数,取决于{{1} }。通常pixel format
为4。来自source code of screencap的信息。
对于你的例子:
bytespp
00000000 d0 02 00 00 00 05 00 00 01 00 00 00 1e 1e 1e ff
- width - uint32 0x000002d0 = 720 d0 02 00 00
- 身高 - uint32 0x00000500 = 1280 00 05 00 00
- 像素格式 - uint32 0x00000001 = 1 = 01 00 00 00
=&gt; PixelFormat.RGBA_8888
=&gt; RGBA bytespp = 4
- 第一个像素数据 - 1e 1e 1e ff
数据存储在字节数组中的像素,大小为720 * 1280 * 4.
答案 1 :(得分:3)
感谢你的文件摘录,我猜你的原始文件是格式化的 width x height然后整个RGBA像素集(32位)(宽x高高倍) 在这里,我看到你拍摄了720x1280的图像..
ImageMagick工具集可以帮助您以更合适的文件格式查看/转换它。 以下是可能对您有所帮助的示例 (ImageMagick convert命令,对于osx,请参阅http://cactuslab.com/imagemagick/)
# skip header info
dd if=screenshot.raw of=screenshot.rgba skip=12 bs=1
# convert rgba to png
convert -size 720x1280 -depth 8 screenshot.rgba screenshot.png
如果它不起作用,你可以尝试通过skip = 8和/或720x1280 by 1280x720改变skip = 12 ..
希望有帮助
答案 2 :(得分:0)
要在python中读取adb screencap原始格式:
from PIL import Image
Image.frombuffer('RGBA', (1920, 1080), raw[12:], 'raw', 'RGBX', 0, 1)
最重要的部分是跳过标题,如@Emmanuel的答案中所述
请注意(1920,1080)是您的设备分辨率,可以通过
获得adb shell wm size
希望这将使某人节省12个小时来调查为什么cv2.matchTemplate在几乎相同的图像上具有不同的匹配项。