What format does "adb screencap /sdcard/screenshot.raw" produce? (without "-p" flag)

Format:

  • 4 bytes as uint32 - width
  • 4 bytes as uint32 - height
  • 4 bytes as uint32 - pixel format
  • (width * heigth * bytespp) bytes as byte array - image data, where bytespp is bytes per pixels and depend on pixel format. Usually bytespp is 4.

Info from source code of screencap.

For your example:

00000000  d0 02 00 00 00 05 00 00  01 00 00 00 1e 1e 1e ff
  • d0 02 00 00 - width - uint32 0x000002d0 = 720
  • 00 05 00 00 - height - uint32 0x00000500 = 1280
  • 01 00 00 00 - pixel format - uint32 0x00000001 = 1 = PixelFormat.RGBA_8888 => bytespp = 4 => RGBA
  • 1e 1e 1e ff - first pixel data - R = 0x1e; G = 0x1e; B = 0x1e; A = 0xff;

Pixels with data stored in array of bytes with size 720*1280*4.


Thanks to the extract of your file , I guess your raw file is formated as width x height then the whole set of RGBA pixels (32 bits) (width x height times) Here I see you get a 720x1280 image captured..

May the ImageMagick toolset help you to view/convert it in a more appropriate file format. Here below a sample that may help you (ImageMagick convert command, for osx see 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

If it doesn't work you may try changing skip=12 by skip=8 and/or 720x1280 by 1280x720 ..

Hope that help


To read adb screencap raw format in python:

from PIL import Image
Image.frombuffer('RGBA', (1920, 1080), raw[12:], 'raw', 'RGBX', 0, 1)

The most important part is skipping the header, as mentioned in @Emmanuel's answer

Note that (1920, 1080) are your device resolution which can be obtained with

adb shell wm size

Hopefully this will save someone 12 hours investigating why cv2.matchTemplate has different match on almost identical images.