pyautogui.screenshot区域不工作

2024-04-27 00:19:38 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图在某个区域捕获屏幕的视频截图时,.avi输出文件突然变成7kb。 没有错误,因此我不知道如何修复此问题 选择区域之前,输出视频正常。 我如何解决这个问题

import cv2
import numpy as np
import pyautogui
# display screen resolution, get it from your OS settings
import tkinter as tk

from PIL.Image import Image

root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
SCREEN_SIZE = (screen_width, screen_height)
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# create the video write object
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))
while True:
    # make a screenshot

    img = pyautogui.screenshot(region=(350, 800, 500, 150))
    # convert these pixels to a proper numpy array to work with OpenCV
    frame = np.array(img)
    # convert colors from BGR to RGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # write the frame
    out.write(frame)
    # show the frame
    cv2.imshow("screenshot", frame)
    # if the user clicks q, it exits
    if cv2.waitKey(1) == ord("q"):
        break

# make sure everything is closed when exited
cv2.destroyAllWindows()
out.release()

Tags: thetofromimport区域视频rootout