import cv2
import numpy as np
import pyautogui
import time
from win32api import GetSystemMetrics
width = GetSystemMetrics(0) #it takes my default system resolution
height = GetSystemMetrics(1)
dim = (width,heigth)#tuple of my default system resolution
f = cv2.VideoWriter_fourcc(*"XVID") # i dont know what this is
output = cv2.VideoWriter("test.mp4",f,60.0,dim)
#name of the video, some stuffs, fps of video, dimension of video
try:
#this loop takes 15 screenshot per second and write it to output variable and its make 15fps
#video
while True:
image = pyautogui.screenshot() #takes screenshot
frame_1 = np.array(image) #converting to arrays
frame = cv2.cvtColor(frame_1,cv2.COLOR_BGR2RGB) #changing the color (rgb)
output. Write(frame)#writes array to output variable
#this loop make it 15 times per second
print(i+"r")
i = time.ctime() # i wrote it 'cause it shows how fast the loop is running per second
except KeyboardInterrupt:
pass
output. Release()
这段代码截取了屏幕截图,并将其写入"输出"变量,然后以60.0fps的速度播放问题是:我的代码每秒拍摄15张屏幕截图,每秒播放60帧,所以当你看视频时,你会很快看到所有东西我该怎么解决?请帮助
pyautogui.screenshot((捕获的时间太长
这个库在我的M1 Mac 中需要250毫秒
您应该使用另一个库以获得更好的性能
像这样检查
class Checktime():
def __init__(self) -> None:
delta_second = 0.0
self.s = datetime.now().second
self.m = datetime.now().microsecond
def delta(self, checkpoint):
d = (datetime.now().second-self.s) + (datetime.now().microsecond-self.m)/1000000
self.s = datetime.now().second
self.m = datetime.now().microsecond
print(checkpoint, d)
try:
#this loop takes 15 screenshot per second and write it to output variable and its make 15fps
#video
c = Checktime()
while (True):
c.delta(1)
image = pyautogui.screenshot() #takes screenshot
c.delta(2)
frame_1 = np.array(image) #converting to arrays
c.delta(3)
frame = cv2.cvtColor(frame_1,cv2.COLOR_BGR2RGB) #changing the color (rgb)
c.delta(4)
output.write(frame)#writes array to output variable
c.delta(5)
#this loop make it 15 times per second
i = time.ctime() # i wrote it 'cause it shows how fast the loop is running per second
c.delta(6)
print(i+"r")
c.delta(7)
结果
1 5e-06
2 0.249821
3 0.00866
4 0.002012
5 3.6e-05
6 1.4e-05
Mon Aug 29 21:23:50 2022
7 7e-06
您可以检查截图((是否在pyautogui 的__init__.py中定义
from pyscreeze import center, grab, pixel, pixelMatchesColor, screenshot
并且在pyscreeze的_init_.py中,您可以检查函数
我找不到屏幕截图的任何延迟选项
您可以直接尝试pyscreeze或使用其他库
def _screenshot_win32(imageFilename=None, region=None):
def _screenshot_osx(imageFilename=None, region=None):
def _screenshot_linux(imageFilename=None, region=None):