11# coding=utf-8
2- '''
2+ """
33Copyright (C) 2012-2022 Diego Torres Milano
44Created on Dec 1, 2012
55
1616limitations under the License.
1717
1818@author: Diego Torres Milano
19- '''
19+ """
2020
2121from __future__ import print_function
2222
2727
2828from com .dtmilano .android .adb .dumpsys import Dumpsys
2929
30- __version__ = '22.4 .0'
30+ __version__ = '22.5 .0'
3131
3232import sys
3333import 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