(Required skill) Screen capture using Python

Article directory

  • (Required skill) Screen capture using Python
    • 1. Preface
    • 2. Environment configuration
      • 1. Download the pyautogui package
      • 2. Download the opencv-python package
      • 3. Download the PyQt5 package
      • 4. Download the pypiwin32 package
    • 3. Screenshot source code and analysis
      • 1. Use the pyautogui method to take screenshots
      • 2. Use PyQt method to take screenshots
        • a. Get the handle of the window, which is the target window name title.
        • b. Use PyQt5 screenshot core program
        • c. Use PyQt5 to screenshot the core program displayed in Mat format
    • 4. Conclusion

1. Preface

There are a lot of pure copies on csdn, which caused me a lot of trouble in reproducing them, so I want to record them based on my personal experience of finding screenshots and show them to my future self, so as not to forget this.
Since I prefer to use opencv to process images, the screenshots will be accompanied by a step of using opencv to display the image.

2. Environment configuration

1. Download the pyautogui package

pip install pyautogui -i https://pypi.tuna.tsinghua.edu.cn/simple/

Note: If you use the pyautogui method to obtain screenshots, just download this one.

2. Download the opencv-python package

pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/

Note: It is convenient for further processing of images, such as real-time acquisition of clouds.

3. Download the PyQt5 package

pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/

Note: Use the PyQt method to take a screenshot and download the introduction

4. Download the pypiwin32 package

pip install pypiwin32 -i https://pypi.tuna.tsinghua.edu.cn/simple/

Note: Use the PyQt method to take a screenshot and download the introduction

3. Screenshot source code and analysis

1. Use the pyautogui method to take screenshots

import pyautogui
import cv2
import numpy as npimg = pyautogui.screenshot(region=[300,50, 200, 100])  # 分别代表:左上角坐标,宽高
#对获取的图片转换成二维矩阵形式,后再将RGB转成BGR
#因为imshow,默认通道顺序是BGR,而pyautogui默认是RGB所以要转换一下,不然会有点问题
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)cv2.imshow("截屏",img)
cv2.waitKey(0)

advantage:

  1. Convenient and fast, easy to write the core part in one line.
  2. The speed is about 0.04s faster, which can basically achieve the effect of real-time screenshots.
  3. You can freely determine the screenshot area

shortcoming:

  1. However, the window of the acquisition program cannot be specified, so the window cannot be blocked.

2. Use PyQt method to take screenshots

a. Get the handle of the window, which is the target window name title.

import win32guihwnd_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)

Note: The program will print the hwnd and title of all windows. With the title, you can take screenshots.

b. Use PyQt5 screenshot core program

from PyQt5.QtWidgets import QApplication
import win32gui
import sys
#这个是截取全屏的
hwnd = win32gui.FindWindow(None, 'C:/Windows/system32/cmd.exe')
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
img = screen.grabWindow(hwnd).toImage()
img.save("screenshot.jpg")

Note: If you want to capture a specific window, just replace C:/Windows/system32/cmd.exe with the title printed in the previous program, and make sure that window is not minimized by you.

advantage:

  1. Convenient and fast, easy to write the core part in one line.
  2. The speed is about 0.04s faster, which can basically achieve the effect of real-time screenshots.
  3. You can freely determine the window to take a screenshot

shortcoming:

  1. Unable to freely determine the screenshot area

c. Use PyQt5 to screenshot the core program displayed in Mat format

def convertQImageToMat(incomingImage):'''  Converts a QImage into an opencv MAT format  '''# Format_RGB32 = 4,存入格式为B,G,R,A 对应 0,1,2,3# RGB32图像每个像素用32比特位表示,占4个字节,# R,G,B分量分别用8个bit表示,存储顺序为B,G,R,最后8个字节保留incomingImage = incomingImage.convertToFormat(4)width = incomingImage.width()height = incomingImage.height()ptr = incomingImage.bits()ptr.setsize(incomingImage.byteCount())arr = np.array(ptr).reshape(height, width, 4)  # Copies the data# arr为BGRA,4通道图片return arrfrom PyQt5.QtWidgets import QApplication
import win32gui
import sys
import cv2
import numpy as np
hwnd = win32gui.FindWindow(None, '剑士之魂中文版小游戏,在线玩,4399小游戏 - 360安全浏览器 13.1')
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
img = screen.grabWindow(hwnd).toImage()img=convertQImageToMat(img)#将获取的图像从QImage转换为RBG格式
cv2.imshow("asd",img)      #imshow
cv2.waitKey(0)

4. Conclusion

After I looked up multiple screenshot methods, I found that you only need to learn these two screenshot methods, so I didn’t include other methods.

Guess you like

Origin blog.csdn.net/qq_41879696/article/details/131993022