environment
windows 10 64bit
python 3.8
mss 6.1.0
foreword
python-mss
It is a very fast screenshot tool that supports cross-platform and is developed in pure python
language .
Install
pip
Install using the command
pip install mss
python-mss
A command line tool is also provided, you can directly capture the screen mss
by , the default is full screen
Other available parameters can be viewed mss -h
through
$ 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 two parts, the first one is the screenshot, which is also its basic function
# 导入模块
import mss
# 实例化对象
with mss.mss() as sct:
# 通过参数output指定保存的文件名
sct.shot(output='output.png')
shot
There is also a parameter in the method callback
, 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 mss
to convert the image format 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')
In the second example, let's see how to record the screen? Here, you need to use it opencv
, just look at the code
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 on Python Practical Modules
For more useful python
modules , please move to
https://xugaoxiang.com/category/python/modules/