Skip to content

Commit a12f029

Browse files
committed
Add box parameter to adbclient take snapshot
- Add screenshot box example
1 parent f65faa4 commit a12f029

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

examples/adbclient/screenshot-box

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#! /usr/bin/env python
2+
3+
import sys
4+
5+
from com.dtmilano.android.viewclient import ViewClient
6+
7+
if len(sys.argv) < 6:
8+
sys.exit("usage: %s left top right bottom filename.png [serialno]" % sys.argv[0])
9+
10+
left = int(sys.argv.pop(1))
11+
top = int(sys.argv.pop(1))
12+
right = int(sys.argv.pop(1))
13+
bottom = int(sys.argv.pop(1))
14+
filename = sys.argv.pop(1)
15+
device, serialno = ViewClient.connectToDeviceOrExit()
16+
device.takeSnapshot(box=(left, top, right, bottom)).save(filename, 'PNG')

src/com/dtmilano/android/adb/adbclient.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding=utf-8
2-
'''
2+
"""
33
Copyright (C) 2012-2022 Diego Torres Milano
44
Created on Dec 1, 2012
55
@@ -16,7 +16,7 @@
1616
limitations under the License.
1717
1818
@author: Diego Torres Milano
19-
'''
19+
"""
2020

2121
from __future__ import print_function
2222

@@ -27,7 +27,7 @@
2727

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

30-
__version__ = '22.4.0'
30+
__version__ = '22.5.0'
3131

3232
import sys
3333
import warnings
@@ -887,10 +887,15 @@ def forceStop(self, package):
887887
if re.search(r"(Error type)|(Error: )", out, re.IGNORECASE | re.MULTILINE):
888888
raise RuntimeError(out)
889889

890-
def takeSnapshot(self, reconnect=False):
891-
'''
890+
def takeSnapshot(self, reconnect=False, box=None):
891+
"""
892892
Takes a snapshot of the device and return it as a PIL Image.
893-
'''
893+
The snapshot is for the entire screen or can be limited to the box if specified.
894+
895+
:param reconnect: reconnects after taking the screenshot
896+
:param box: box as a tuple indicating (left, top, right, bottom) to crop the entire screen
897+
:returns: the image
898+
"""
894899

895900
if PROFILE:
896901
profileStart()
@@ -901,7 +906,7 @@ def takeSnapshot(self, reconnect=False):
901906
global Image
902907
from PIL import Image
903908
PIL_AVAILABLE = True
904-
except:
909+
except ImportError:
905910
raise Exception("You have to install PIL to use takeSnapshot()")
906911

907912
sdk_version = self.getSdkVersion()
@@ -993,11 +998,13 @@ def takeSnapshot(self, reconnect=False):
993998
r = (0, 90, 180, -90)[self.display['orientation']]
994999
else:
9951000
r = 90
996-
image = image.rotate(r, expand=1).resize((h, w))
1001+
image = image.rotate(r, expand=True).resize((h, w))
9971002

9981003
if PROFILE:
9991004
profileEnd()
10001005
self.screenshot_number += 1
1006+
if box:
1007+
return image.crop(box)
10011008
return image
10021009

10031010
def imageToData(self, image, output_type=None):

0 commit comments

Comments
 (0)