Two ways that Python implements screenshots

Source: Internet
Author: User

There are two ways in which Python implements the screen:

    • Using the Windows API
    • Using the Imagegrab module in PIL

The characteristics and usage of the two are explained in detail below.

One, Python calls the Windows API implementation screen

Advantage is

    • Flexible
    • Fast speed

The disadvantages are:

    • The tedious wording
    • Non-cross platform
ImportTimeImportWin32gui, Win32ui, Win32con, Win32APIdefWindow_capture (filename): HWND= 0  # Number of the window, number No. 0 indicates the current active window    # Gets the device context of the window according to the window handle DC (Divice context)Hwnddc=Win32gui. GETWINDOWDC (HWND)# Get MFCDC based on the DC of the windowMfcdc=Win32ui. Createdcfromhandle (HWNDDC)# MFCDC To create a compatible DCSaveDC=Mfcdc.createcompatibledc ()# Create Bigmap prepare to save pictureSavebitmap=Win32ui. CreateBitmap ()# get monitor informationMoniterdev=Win32API. Enumdisplaymonitors (None,None) W=moniterdev[0][2][2] H=moniterdev[0][2][3]# print W,h #图片大小    # Open up space for bitmapSavebitmap.createcompatiblebitmap (MFCDC, W, h)# height SaveDC, will be saved to SavebitmapSavedc.selectobject (Savebitmap)# Capture a picture from the upper left (0,0) width to (w,h)Savedc.bitblt ((0,0), (W, h), MFCDC, (0,0), Win32con. srccopy) savebitmap.savebitmapfile (SAVEDC, filename) beg=Time.time () forIinch Range(Ten): Window_capture ("Haha.jpg") End=Time.time ()Print(End-Beg

The output is 0.375 seconds, which means that the Windows API only needs 0.0375s at a time, so it's a quick push.

Second, the use of PIL Imagegrab module
import  timeimport  numpy as  npfrom  PIL import  imagegrab # takes about 1s each time a screen is fetched, and if the image size is smaller, the efficiency will be higher.  Beg =  time.time () Debug =  false  for  i in  range  (10 ): img =  imagegrab.grab (Bbox=  (250 , 161 , 1141 , 610 )) img =  Np.array (Img.getdata (), np.uint8). Reshape (img.size[< Span class= "DV" >1 ], Img.size[0 ], 3 ) End =  time.time () print  (End - Beg) 

The output is 4.015 seconds, which means that it takes half a second to intercept the screen, not to mention that only a portion of the window's rectangular area is intercepted here. So the Imagegrab module can be used to be able to use, that is a bit slow.

Third, the use of selenium

Web programs can only be used with selenium
Each browser that uses selenium needs to develop the driver's location, drive see Selenium official website

 fromSeleniumImportWebdriverImportTimedefCapture (URL, filename="Capture.png"): Browser=Webdriver. Chrome (r "C:\Users\weidiao\Desktop\chromedriver_win32\chromedriver.exe") Browser.set_window_size ( -, the) Browser.get (URL)# Load PageBrowser.execute_script ("""(function () {var y = 0;var step = +;window.scroll (0, 0);function f () {if (Y < document.body.scrollHeight) {y + = step;window.scroll (0, y);SetTimeout (f, +);} else {window.scroll (0, 0);Document.title + = "Scroll-done";        }      }SetTimeout (f, +);    })();  """) forIinch Range( -):if "Scroll-done" inchBrowser.title: BreakTime.sleep (1) Beg=Time.time () forIinch Range(Ten): browser.save_screenshot (filename) End=Time.time ()Print(End-Beg) Browser.close () Capture ("Http://www.cnblogs.com/weidiao")

The print time is 3.033s and the speed is between the Windows API and the PiL Imagegrab module.

Two ways Python implements the screen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.