Imagine that you are developing an application, where, after certain time interval, the program needs to automatically capture the whole screen or a part of the screen. Let's develop code that achieves this.
Write the following code in a Python source file. When the code is executed, it will capture part of the screen after every two seconds. The code will run for about three seconds.
1 import ImageGrab 2 import time 3 startTime = time.clock() 4 print "\n The start time is %s sec" % startTime 5 # Define the four corners of the bounding box. 6 # (in pixels) 7 left = 150 8 upper = 200 9 right = 900 10 lower = 700 11 bbox = (left, upper, right, lower) 12 13 while time.clock() < 3: 14 print " \n Capturing screen at time %.4f sec" \ 15 %time.clock() 16 screenShot = ImageGrab.grab(bbox) 17 name = str("%.2f"%time.clock())+ "sec.png" 18 screenShot.save("C:\\images\\output\\" + name) 19 time.sleep(2)
We will now review the important...