One click allows you to use screen recording

I am working on a project recently, and sometimes I need to record the screen, but my operation is very slow, and I often have to count the minutes at every turn. What judge teacher can stand this? So I wrote a screen recording code using opencv, which is very easy to use. The code is as follows:

import numpy as np
from PIL import ImageGrab
import cv2

im = ImageGrab.grab()
width, high = im.size  # 获取屏幕的宽和高
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')  # 设置视频编码格式
fps = 30  # 设置帧率
video = cv2.VideoWriter('test.mp4', fourcc, fps, (width, high))
while True:  # 开始录制
    im = ImageGrab.grab()
    im_cv = cv2.cvtColor(np.array(im), cv2.COLOR_BGR2RGB)
    # 图像写入
    video.write(im_cv)
    cv2.imshow('1',im_cv)
    if cv2.waitKey(1) in [ord('q'),27]:  # 当某某条件满足中断循环
        break
video.release()  # 释放缓存,持久化视频

It is recommended to put the code on the desktop so that the recorded video is also on the desktop for easy search. The name of the video is test.mp4. After the recording is completed, press the esc or q key in English mode to end.

Guess you like

Origin blog.csdn.net/qq_54932411/article/details/132712889