def capture_as_image(self, rect=None):"""Return a PIL image of the control.
See PIL documentation to know what you can do with the resulting
image."""control_rectangle=self.rectangle()if not (control_rectangle.width() andcontrol_rectangle.height()):returnNone#PIL is optional so check first
if notImageGrab:print("PIL does not seem to be installed."
"PIL is required for capture_as_image")
self.actions.log("PIL does not seem to be installed."
"PIL is required for capture_as_image")returnNoneifrect:
control_rectangle=rect#get the control rectangle in a way that PIL likes it
width =control_rectangle.width()
height=control_rectangle.height()
left=control_rectangle.left
right=control_rectangle.right
top=control_rectangle.top
bottom=control_rectangle.bottom
box=(left, top, right, bottom)#check the number of monitors connected
if (sys.platform == 'win32') and (len(win32api.EnumDisplayMonitors()) > 1):
hwin=win32gui.GetDesktopWindow()
hwindc=win32gui.GetWindowDC(hwin)
srcdc=win32ui.CreateDCFromHandle(hwindc)
memdc=srcdc.CreateCompatibleDC()
bmp=win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.SelectObject(bmp)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
bmpinfo=bmp.GetInfo()
bmpstr=bmp.GetBitmapBits(True)
pil_img_obj= Image.frombuffer('RGB',
(bmpinfo['bmWidth'], bmpinfo['bmHeight']),
bmpstr,'raw','BGRX',
0,1)else:#grab the image and get raw data as a string
pil_img_obj =ImageGrab.grab(box)return pil_img_obj