Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to realize screenshot

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this article, the editor introduces in detail "how to use Python to achieve screen capture", the content is detailed, the steps are clear, and the details are handled properly. I hope this "how to use Python to achieve screen capture" article can help you solve your doubts, following the editor's ideas slowly in depth, let's learn new knowledge.

I. Preface

There are a lot of pure copy on csdn, which brings me a lot of trouble to reproduce, so I want to record it according to my personal experience of finding screenshots and show them to myself in the future, so as not to forget and so on.

II. Environmental 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 get a screenshot, just download this.

2. Download the opencv-python package

Pip install opencv-python-I https://pypi.tuna.tsinghua.edu.cn/simple/

Note: it is convenient to realize the further processing of pictures, such as real-time acquisition and so on.

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 to download a

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 to download a

Third, the source code and analysis of screen capture

1. Use pyautogui method to achieve screenshot.

Import pyautoguiimport cv2import numpy as npimg = pyautogui.screenshot (region= [300JE50,200,100]) # represents: upper left coordinates, width and height # pairs of acquired images are converted into two-dimensional matrix form, and then RGB is converted to BGR# because imshow, the default channel order is BGR, and pyautogui defaults to RGB, so you have to convert it, otherwise there will be some problems img = cv2.cvtColor (np.asarray (img), cv2.COLOR_RGB2BGR) cv2.imshow ("Screenshot", img) cv2.waitKey (0)

Advantages:

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

Disadvantages:

However, the window to get the program cannot be specified, so the window cannot be obscured. 2. Use PyQt method to realize screenshot a. Gets the handle to the window, that is, the target window name title.

Import win32guihwnd_title = dict () # create the mapping relationship between handle and name of the dictionary saving window 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 tweak = "": print (h, t)

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

b. Use PyQt5 screenshot core program

From PyQt5.QtWidgets import QApplicationimport win32guiimport sys# this is a full screen capture of hwnd = win32gui.FindWindow (None, 'CJV') app = QApplication (sys.argv) screen = QApplication.primaryScreen () img = screen.grabWindow (hwnd). ToImage () img.save ("screenshot.jpg")

Note: if you want to intercept 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.

Advantages:

Convenient and fast, easy to write the core part on one line. The speed is about 0.04s, which can basically achieve the effect of real-time screenshot. You are free to determine the window to take screenshots.

Disadvantages:

The screenshot area c. Core programs displayed in Mat format using PyQt5 screenshots

Def convertQImageToMat (incomingImage):''Converts a QImage into an opencv MAT format''# Format_RGB32 = 4, stored in the format of B bit, corresponding to 0Magi 1, Magi 2, 3 # RGB32 image, each pixel is represented by 32 bits, accounting for 4 bytes, and the # R GMagee B component is represented by 8 BMague respectively, and the storage order is BMageGMagee R The last 8 bytes keep 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 as BGRA 4-channel picture return arrfrom PyQt5.QtWidgets import QApplicationimport win32guiimport sysimport cv2import numpy as nphwnd = win32gui.FindWindow (None, 'Soul of the Swordsman Chinese version of Mini Game, online play, 4399 Mini Game-360Security browser 13.1') app = QApplication (sys.argv) screen = QApplication.primaryScreen () img= screen.grabWindow (hwnd). ToImage () img=convertQImageToMat (img) # convert the captured image from QImage to RBG format cv2.imshow ("asd", img) # imshowcv2.waitKey (0) to read here This article "how to use Python to achieve screen capture" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report