From a12f029a606116ea64582a76991712fc21e4272b Mon Sep 17 00:00:00 2001 From: Diego Torres Milano Date: Sun, 25 Dec 2022 21:57:23 -0800 Subject: [PATCH] Add box parameter to adbclient take snapshot - Add screenshot box example --- examples/adbclient/screenshot-box | 16 ++++++++++++++++ src/com/dtmilano/android/adb/adbclient.py | 23 +++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 examples/adbclient/screenshot-box diff --git a/examples/adbclient/screenshot-box b/examples/adbclient/screenshot-box new file mode 100644 index 00000000..5ddf6806 --- /dev/null +++ b/examples/adbclient/screenshot-box @@ -0,0 +1,16 @@ +#! /usr/bin/env python + +import sys + +from com.dtmilano.android.viewclient import ViewClient + +if len(sys.argv) < 6: + sys.exit("usage: %s left top right bottom filename.png [serialno]" % sys.argv[0]) + +left = int(sys.argv.pop(1)) +top = int(sys.argv.pop(1)) +right = int(sys.argv.pop(1)) +bottom = int(sys.argv.pop(1)) +filename = sys.argv.pop(1) +device, serialno = ViewClient.connectToDeviceOrExit() +device.takeSnapshot(box=(left, top, right, bottom)).save(filename, 'PNG') diff --git a/src/com/dtmilano/android/adb/adbclient.py b/src/com/dtmilano/android/adb/adbclient.py index 53cc5d8f..7bf56ec1 100644 --- a/src/com/dtmilano/android/adb/adbclient.py +++ b/src/com/dtmilano/android/adb/adbclient.py @@ -1,5 +1,5 @@ # coding=utf-8 -''' +""" Copyright (C) 2012-2022 Diego Torres Milano Created on Dec 1, 2012 @@ -16,7 +16,7 @@ limitations under the License. @author: Diego Torres Milano -''' +""" from __future__ import print_function @@ -27,7 +27,7 @@ from com.dtmilano.android.adb.dumpsys import Dumpsys -__version__ = '22.4.0' +__version__ = '22.5.0' import sys import warnings @@ -887,10 +887,15 @@ def forceStop(self, package): if re.search(r"(Error type)|(Error: )", out, re.IGNORECASE | re.MULTILINE): raise RuntimeError(out) - def takeSnapshot(self, reconnect=False): - ''' + def takeSnapshot(self, reconnect=False, box=None): + """ Takes a snapshot of the device and return it as a PIL Image. - ''' + The snapshot is for the entire screen or can be limited to the box if specified. + + :param reconnect: reconnects after taking the screenshot + :param box: box as a tuple indicating (left, top, right, bottom) to crop the entire screen + :returns: the image + """ if PROFILE: profileStart() @@ -901,7 +906,7 @@ def takeSnapshot(self, reconnect=False): global Image from PIL import Image PIL_AVAILABLE = True - except: + except ImportError: raise Exception("You have to install PIL to use takeSnapshot()") sdk_version = self.getSdkVersion() @@ -993,11 +998,13 @@ def takeSnapshot(self, reconnect=False): r = (0, 90, 180, -90)[self.display['orientation']] else: r = 90 - image = image.rotate(r, expand=1).resize((h, w)) + image = image.rotate(r, expand=True).resize((h, w)) if PROFILE: profileEnd() self.screenshot_number += 1 + if box: + return image.crop(box) return image def imageToData(self, image, output_type=None):