Skip to content

Commit

Permalink
Add box parameter to adbclient take snapshot
Browse files Browse the repository at this point in the history
- Add screenshot box example
  • Loading branch information
dtmilano committed Dec 26, 2022
1 parent f65faa4 commit a12f029
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
16 changes: 16 additions & 0 deletions examples/adbclient/screenshot-box
Original file line number Diff line number Diff line change
@@ -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')
23 changes: 15 additions & 8 deletions src/com/dtmilano/android/adb/adbclient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding=utf-8
'''
"""
Copyright (C) 2012-2022 Diego Torres Milano
Created on Dec 1, 2012
Expand All @@ -16,7 +16,7 @@
limitations under the License.
@author: Diego Torres Milano
'''
"""

from __future__ import print_function

Expand All @@ -27,7 +27,7 @@

from com.dtmilano.android.adb.dumpsys import Dumpsys

__version__ = '22.4.0'
__version__ = '22.5.0'

import sys
import warnings
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit a12f029

Please sign in to comment.