Using Python to Implement Screenshots
Environmental configuration
Download pyautogui package
pip install pyautogui -i https://pypi.tuna.tsinghua.edu.cn/simple/
Download OpenCV package
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/
Download PyQT5 package
pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/
Download pypiwin32 package
pip install pypiwin32 -i https://pypi.tuna.tsinghua.edu.cn/simple/
Specific implementation
[1]
explain.
- Use
pyautoguimethod to achieve screenshot;
code.
import pyautogui
import cv2
import numpy as np
#the following numbers represent : top left horizontal coordinate, top left vertical coordinate, capture image width, capture image height ;
img = pyautogui.screenshot(region=[0, 0, 1902, 1080])
#convert the obtained image into a two-dimensional matrix form, and then RGB convert into BGR
#because `imshow` the default channel order is `BGR` and `pyautogui` default is `RGB` so we need to convert it
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
cv2.imshow("screenshot", img)
cv2.waitKey(0)
annotation.
- This method cannot specify the window for obtaining the specified program, so the window cannot be obscured;
[2]
explain.
- Use
win32guimethod to achieve screenshot;
code.
"1".
- Obtain the handle and title of the target program window;
- Print
hwndandtitlefor all windows; - You can take a specified screenshot based on the window handle;
import win32gui
#create handle and name mapping relationship for dictionary save window
hwnd_title = dict()
def get_all_hwnd(hwnd, mouse):
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})
win32gui.EnumWindows(get_all_hwnd, 0)
for h, t in hwnd_title.items():
if t != "":
print(h, t)
import win32gui
# GetDesktopWindow obtain a window (desktop window) handle representing the entire screen
hd = win32gui.GetDesktopWindow()
#get all sub windows
hwndChildList = []
win32gui.EnumChildWindows(hd, lambda hwnd, param: param.append(hwnd), hwndChildList)
for hwnd in hwndChildList:
print("handle :", hwnd, "title :", win32gui.GetWindowText(hwnd))
# f.write("handle :" + str(hwnd) + "title :" + win32gui.GetWindowText(hwnd) + 'n')
result.
3802250 mouseControle – OpenCVDemo.py
3278598 this computer
"2".
- Use
PyQt5for full screen capture operation; - If you want to capture a specific window, simply replace
C:/Windows/system32/cmd.exewith thetitleprinted in the previous program and ensure that the window is not minimized by you;
code.
import sys
import win32gui
from PyQt5.QtWidgets import QApplication
#this is a full screen window
hwnd = win32gui.FindWindow(None, 'C:/Windows/system32/cmd.exe')
#this is the designated program
# hwnd = win32gui.FindWindow(None, win32gui.GetWindowText(3212524))
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
img = screen.grabWindow(hwnd).toImage()
img.save(r"C:UsersSUNxRUNDesktopscreenshot.jpg")
#front window win32gui.SetForegroundWindow(hwnd)
"3".
- Temporarily abandoned;
- Real time use of
win32guiscreenshots to display the core program in Mat format;
code.
import win32gui
import cv2
import numpy as np
from PIL import ImageGrab #operation image
hwnd = win32gui.FindWindow(None, 'QQMail - Inbox -360 speed browser X 21.0')#the second parameter requires the use of two a run that program to obtain
while True:
x_start, y_start, x_end, y_end = win32gui.GetWindowRect(hwnd)
#coordinate information
box = (x_start, y_start, x_end, y_end)
image = ImageGrab.grab(box)
img=cv2.cvtColor(np.asarray(image),cv2.COLOR_RGB2BGR)
cv2.imshow('Img',img)
cv2.waitKey(1)