Package lib :: Package api :: Module screenshot
[hide private]
[frames] | no frames]

Source Code for Module lib.api.screenshot

 1  # Copyright (C) 2010-2014 Cuckoo Foundation. 
 2  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org 
 3  # See the file 'docs/LICENSE' for copying permission. 
 4   
 5  import math 
 6   
 7  try: 
 8      import ImageChops 
 9      import ImageGrab 
10      HAVE_PIL = True 
11  except: 
12      HAVE_PIL = False 
13   
14 -class Screenshot:
15 """Get screenshots.""" 16
17 - def have_pil(self):
18 """Is Python Image Library installed? 19 @return: installed status. 20 """ 21 return HAVE_PIL
22
23 - def equal(self, img1, img2):
24 """Compares two screenshots using Root-Mean-Square Difference (RMS). 25 @param img1: screenshot to compare. 26 @param img2: screenshot to compare. 27 @return: equal status. 28 """ 29 if not HAVE_PIL: 30 return None 31 32 # To get a measure of how similar two images are, we use 33 # root-mean-square (RMS). If the images are exactly identical, 34 # this value is zero. 35 diff = ImageChops.difference(img1, img2) 36 h = diff.histogram() 37 sq = (value*((idx%256)**2) for idx, value in enumerate(h)) 38 sum_of_squares = sum(sq) 39 rms = math.sqrt(sum_of_squares/float(img1.size[0] * img1.size[1])) 40 41 # Might need to tweak the threshold. 42 return rms < 8
43
44 - def take(self):
45 """Take a screenshot. 46 @return: screenshot or None. 47 """ 48 if not HAVE_PIL: 49 return None 50 51 return ImageGrab.grab()
52