pyautogui.screenshot区域无法正常工作

时间:2020-06-04 07:06:34

标签: python python-3.x opencv pyautogui

当我尝试捕获某个区域的屏幕视频截图时,.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()

0 个答案:

没有答案