Python screen is implemented in two ways: python Screen
- Use windows APIs
- Use the ImageGrab module in PIL
The following describes the features and usage of the two.
I. Python calls windows API to implement the screen
The advantage is that
Disadvantages:
- Tedious writing
- Not cross-platform
Import timeimport win32gui, win32ui, win32con, win32apidef window_capture (filename): hwnd = 0 # window number, 0 indicates the current active window # obtain the device Context DC (Divice Context) hwndDC = win32gui Of The Window Based on the window handle. getWindowDC (hwnd) # obtain mfcDC = win32ui Based on the window DC. createDCFromHandle (hwndDC) # mfcDC creates compatible DC saveDC = mfcDC. createCompatibleDC () # create bigmap and prepare to save the image saveBitMap = win32ui. createBitmap () # obtain the monitor information MoniterDev = win32api. enumDisplayMonitors (None, None) w = MoniterDev [0] [2] [2] h = MoniterDev [0] [2] [3] # print w, h # image size # create a space for bitmap saveBitMap. createCompatibleBitmap (mfcDC, w, h) # The saveDC height is saved to saveBitmap. selectObject (saveBitMap) # extract the saveDC image with the length and width (w, h) from the upper left corner. bitBlt (0, 0), (w, h), mfcDC, (0, 0), win32con. SRCCOPY) saveBitMap. saveBitmapFile (saveDC, filename) beg = time. time () for I in range (10): window_capture ("haha.jpg") end = time. time () print (end-beg)
The output is 0.375 seconds. That is to say, it takes only 0.0375 seconds to use windows APIs.
Ii. Use PIL's ImageGrab Module
Import timeimport numpy as npfrom PIL import ImageGrab # the time required for each capture of the screen is about 1 s. If the image size is small, the efficiency will be higher. beg = time. time () debug = Falsefor I in range (10): img = ImageGrab. grab (bbox = (250,161,114 1, 610) img = np. array (img. getdata (), np. uint8 ). reshape (img. size [1], img. size [0], 3) end = time. time () print (end-beg)
The output is 4.015 seconds. That is to say, it takes half a second to capture a screen. In addition, only part of the rectangular area of the window is intercepted. Therefore, the ImageGrab module can be used or used, which is a little slow.
3. Use Selenium
Only WEB programs can be used with Selenium
To use Selenium browsers, you must specify the driver location. For more information about the driver, see Selenium official website.
from selenium import webdriverimport timedef capture(url, filename="capture.png"): browser = webdriver.Chrome(r"C:\Users\weidiao\Desktop\chromedriver_win32\chromedriver.exe") browser.set_window_size(1200, 900) browser.get(url) # Load page browser.execute_script(""" (function () { var y = 0; var step = 100; window.scroll(0, 0); function f() { if (y < document.body.scrollHeight) { y += step; window.scroll(0, y); setTimeout(f, 50); } else { window.scroll(0, 0); document.title += "scroll-done"; } } setTimeout(f, 1000); })(); """) for i in range(30): if "scroll-done" in browser.title: break time.sleep(1) beg = time.time() for i in range(10): browser.save_screenshot(filename) end = time.time() print(end - beg) browser.close()capture("http://www.bkjia.com")
The printing time is 3.033 s, and the speed is between windows API and PIL ImageGrab module.
Ps: Let's take a look at the python basics === python implementation.
Python full screen:
from PIL import ImageGrabim = ImageGrab.grab()im.save('F:\\12.png')
Summary
The above is a small Editor to introduce you to the Python implementation screen two ways, I hope to help you, if you have any questions, please leave a message, the small editor will reply to you in a timely manner. Thank you very much for your support for the help House website!