python——录制屏幕

录制屏幕的使用

import cv2
import numpy as np
import pyautogui

# 设置屏幕大小
screen_size = (1920, 1080)

# 设置视频编码器
fourcc = cv2.VideoWriter_fourcc(*"XVID")

# 创建视频写入器q
out = cv2.VideoWriter("output.avi", fourcc, 20.0, screen_size)

while True:
    # 获取屏幕截图
    img = pyautogui.screenshot()
    
    # 转换为OpenCV格式
    frame = np.array(img)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    
    # 写入视频
    out.write(frame)
    
    # 显示视频
    cv2.imshow("screen", frame)
    
    # 按q键退出
    if cv2.waitKey(1) == ord("q"):
        print("==录屏结束==")
        break
        
# 释放资源
cv2.destroyAllWindows()
out.release()

使用pyautogui.screenshot()来进行截图,相对来说比较的清晰,推荐

你可能感兴趣的:(python,python,opencv,numpy)