问题
i am creating a screen recorder for desktop in python. and i have already completed the coding part but when i record the screen why it is not recording in high defination. problem......... 1> i want to record the screen in high quality what should i do. 2> i am also trying to capute my cursor with the help on cv2.circle() but i want to create the circle with less opacity or more transparent
my code
import cv2
import numpy as np
import pyautogui
import datetime
import win32api
date=datetime.datetime.now()
SCREEN_SIZE = (960,540) #std res 1366, 768
framerate=12
# define the codec
fourcc = cv2.VideoWriter_fourcc(*'XVID')
filename='E:/project/videos/rec_%s%s%s%s%s%s.avi' %(date.year,date.month,date.day,date.hour,date.minute,date.second)
out = cv2.VideoWriter(filename, fourcc,framerate, SCREEN_SIZE)
while True:
img = pyautogui.screenshot()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
_xs,_ys = win32api.GetCursorPos()
image=cv2.circle(frame,(_xs,_ys),20,(0,255,255,0.3),-1)
frame = cv2.resize(frame,(960,540)) #resize window
out.write(frame)
cv2.imshow('screenshot', frame)
if cv2.waitKey(1) == ord("q"):
break
cv2.destroyAllWindows()
out.release()
回答1:
When you use cv2.videowriter
, make sure that the framesize is equal to the size of the screen so that the image is not distorted.
In addition, if you need to record the screen, you should make sure that the opencv window does not affect the beauty of the entire desktop.
In my code, I have added a translucent circle. You can change the transparency of the circle as needed.
from PIL import ImageGrab
import numpy as np
import cv2
import datetime
from pynput import keyboard
import threading
import win32api
flag=False
def transparent_circle(img,center,radius,color,thickness):
center = tuple(map(int,center))
rgb = [255*c for c in color[:3]] # convert to 0-255 scale for OpenCV
alpha = color[-1]
radius = int(radius)
if thickness > 0:
pad = radius + 2 + thickness
else:
pad = radius + 3
roi = slice(center[1]-pad,center[1]+pad),slice(center[0]-pad,center[0]+pad)
try:
overlay = img[roi].copy()
cv2.circle(img,center,radius,rgb, thickness=thickness, lineType=cv2.LINE_AA)
opacity = alpha
cv2.addWeighted(src1=img[roi], alpha=opacity, src2=overlay, beta=1. - opacity, gamma=0, dst=img[roi])
except:
logger.debug("transparent_circle would have been partially outside of img. Did not draw it.")
def video_record():
date=datetime.datetime.now()
filename='path/rec_%s%s%s%s%s%s.avi' %(date.year,date.month,date.day,date.hour,date.minute,date.second)
p = ImageGrab.grab()
a, b = p.size
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video = cv2.VideoWriter(filename, fourcc, 20, (a, b))
while True:
im = ImageGrab.grab()
imm=cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)
_xs,_ys = win32api.GetCursorPos()
transparent_circle(imm,(_xs,_ys),20,(0,255,255,0.5), -1)
# cv2.circle(imm,(_xs,_ys),20,(0,255,255,0.3),-1)
video.write(imm)
if flag:
print("Record Over")
break
video.release()
def on_press(key):
global flag
if key == keyboard.Key.esc:
flag=True
print("stop monitor")
return False
if __name__=='__main__':
th=threading.Thread(target=video_record)
th.start()
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
When you run the code, start recording. When you press the ESC
key, stop recording and save the file.
Note: You may need to add some libraries, otherwise there will be an error that the module cannot be found
来源:https://stackoverflow.com/questions/60300923/how-to-record-screen-in-high-quality