surroundings
- windows 10 64bit
- python 3.8
- mss 6.1.0
foreword
python -mss
is a very fast screenshot tool that supports cross-platform and is developed in pure python
language.
Install
Install using pip
, execute the command
pip install mss
python -mss
also provides a command line tool, you can directly capture the screen using mss
, the default is full screen
Other available parameters can be viewed through mss -h
$ mss -h usage: mss [-h] [-c COORDINATES] [-l {0,1,2,3,4,5,6,7,8,9}] [-m MONITOR] [-o OUTPUT] [-q] [-v] optional arguments: -h, --help show this help message and exit -c COORDINATES, --coordinates COORDINATES the part of the screen to capture: top, left, width, height -l {0,1,2,3,4,5,6,7,8,9}, --level {0,1,2,3,4,5,6,7,8,9} the PNG compression level -m MONITOR, --monitor MONITOR the monitor to screen shot -o OUTPUT, --output OUTPUT the output file name -q, --quiet do not print created files -v, --version show program's version number and exit
Practical
Let’s look at the content of 2 parts, the first one is a screenshot, which is also its basic function
# 导入模块import mss # 实例化对象with mss.mss() as sct: # 通过参数output指定保存的文件名sct.shot(output='output.png')
There is also a parameter callback
in the shot
method, which is convenient for us to use the callback
import mss def fun_callback(filename): # 作为示例,打印下截取的文件名print(filename) with mss.mss() as sct: # 通过参数output指定保存的文件名filename = sct.shot(output='output.png', callback=fun_callback)
If it is to intercept part of the window
import mss with mss.mss() as sct: # 设置一个区域,top为距离屏幕左上角的垂直方向上的距离,left是水平方向的距离,后面2个分别是宽和高monitor = {"top":50, "left":50, "width": 600, "height": 400} image_sct = sct.grab(monitor) # 转换成png保存起来mss.tools.to_png(image_sct.rgb, image_sct.size, output="output.png")
If you need to convert the image format of mss
to PIL
, you can do this
import mss from PIL import Image with mss.mss() as sct: # 设置一个区域,top为距离屏幕左上角的垂直方向上的距离,left是水平方向的距离,后面2个分别是宽和高monitor = {"top":50, "left":50, "width": 600, "height": 400} image_sct = sct.grab(monitor) image_pil = Image.frombytes('RGB', image_sct.size, image_sct.bgra, 'raw', 'BGRX') image_pil.save('output_pil.png')
The second example, let’s see how to record the screen? Here, you need to use opencv
to see the code directly
import mss import cv2 import numpy as np from PIL import Image monitor = {"top":50, "left":50, "width": 600, "height": 400} fourcc = cv2.VideoWriter_fourcc(*'MJPG') fps = 20 video = cv2.VideoWriter('output.avi', fourcc, fps, (600, 400)) with mss.mss() as sct: # 设置一个区域,top为距离屏幕左上角的垂直方向上的距离,left是水平方向的距离,后面2个分别是宽和高while True: image_sct = sct.grab(monitor) image_pil = Image.frombytes('RGB', image_sct.size, image_sct.bgra, 'raw', 'BGRX') image_cv = np.array(image_pil) video.write(image_cv) if cv2.waitKey(25) & 0xFF == ord('q'): break video.release()
For more details, please refer to the official link https://github.com/BoboTiG/python-mss
Topics in Python Practical Modules
More useful python
modules, please move
https://xugaoxiang.com/category/python/modules/
This article is reprinted from https://xugaoxiang.com/2022/09/22/python-module-37-mss/
This site is for inclusion only, and the copyright belongs to the original author.