通常录屏这个事情,我们都是使用商业化、功能完备的录屏软件进行录屏。今天我们不用这些产品,直接使用Python进行录屏操作。在写程序之前,我们需要安装opencv-python,mss,numpy这些第三方库。完成安装之后,就可以编写以下程序了。
import cv2
import mss
import numpy
with mss.mss() as sct:
# Part of the screen to capture
monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
while "Screen capturing":
last_time = time.time()
# Get raw pixels from the screen, save it to a Numpy array
img = numpy.array(sct.grab(monitor))
# Display the picture
cv2.imshow("OpenCV/Numpy normal", img)
# Display the picture in grayscale
# cv2.imshow('OpenCV/Numpy grayscale',
# cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))
print("fps: {}".format(1 / (time.time() - last_time)))
# Press "q" to quit
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break
今天的分享就到这里,希望,对你有启发,在编程的道路上能帮到你。