python实现录制全屏和选择区域录屏功能
作者:nihate 时间:2022-06-18 01:06:33
最近给客户演示程序运行结果,我就想到用Python写一个录屏程序,在网上能找到现成的源码,但是它的录屏是录制整个屏幕的。但是在屏幕桌面下方的任务栏工具栏里有些东西,不希望被录制到视频里,因此需要实现一个选择区域录屏,就像qq截图那样的。我编写的程序如下,在主函数的输入参数里有一个选项控制是全屏录制还是选择区域录制。在编写这个程序时,我有一个疑问,在初始化写视频VideoWriter函数的第4个参数,它表示视频帧的高和宽,
全屏录制方式的参数是(height,width),选择区域录制的参数是(width,height)。我曾尝试过把这个参数统一写成(height,width)或者(width,height)。但是运行程序后,总有一种录屏方式保存的视频文件是无法打开的,文件大小是1kb,不知这是为何?
import cv2
from PIL import ImageGrab
import numpy as np
import argparse
import time
global img
global point1, point2
def on_mouse(event, x, y, flags, param):
global img, point1, point2
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # 左键点击
point1 = (x, y)
cv2.circle(img2, point1, 10, (0, 255, 0), thickness=2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳
cv2.rectangle(img2, point1, (x, y), (255, 0, 0), thickness=2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_LBUTTONUP: # 左键释放
point2 = (x, y)
cv2.rectangle(img2, point1, point2, (0, 0, 255), thickness=2)
cv2.imshow('image', img2)
def select_roi(frame):
global img, point1, point2
img = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)
winname = 'image'
cv2.namedWindow(winname, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(winname, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.setMouseCallback(winname, on_mouse)
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return point1, point2
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--fps', type=int, default=10, help='frame per second')
parser.add_argument('--total_time', type=int, default=15, help='video total time')
parser.add_argument('--savename', type=str, default='video.mp4', help='save file name')
parser.add_argument('--screen_type', default=1, type=int, choices=[0, 1], help='1: full screen, 0: region screen')
args = parser.parse_args()
print('等到3秒,请切换到录屏的页面')
if args.screen_type==0:
print('Press Esc to close window')
time.sleep(3)
curScreen = ImageGrab.grab() # 获取屏幕对象
if args.screen_type:
height, width = curScreen.size
min_x, min_y, max_x, max_y = 0, 0, width, height
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(args.savename, fourcc, args.fps, (height, width))
else:
point1, point2 = select_roi(curScreen)
min_x = min(point1[0], point2[0])
min_y = min(point1[1], point2[1])
max_x = max(point1[0], point2[0])
max_y = max(point1[1], point2[1])
height, width = max_y - min_y, max_x - min_x
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(args.savename, fourcc, args.fps, (width, height))
imageNum = 0
while True:
imageNum += 1
captureImage = ImageGrab.grab() # 抓取屏幕
frame = cv2.cvtColor(np.array(captureImage), cv2.COLOR_RGB2BGR)
if args.screen_type==0:
frame = frame[min_y:max_y, min_x:max_x, :]
if imageNum < args.fps * args.total_time:
video.write(frame)
# 退出条件
if cv2.waitKey(50) == ord('q') or imageNum > args.fps * args.total_time:
break
video.release()
cv2.destroyAllWindows()
来源:https://blog.csdn.net/nihate/article/details/113662290
标签:python,录屏,选择区域录屏
0
投稿
猜你喜欢
ASP中使用SQL语句教程
2008-09-03 12:17:00
浏览器针对单服务器连接数问题
2008-05-12 22:27:00
python统计cpu利用率的方法
2022-02-05 23:33:44
sql ROW_NUMBER()与OVER()方法案例详解
2024-01-12 21:34:32
AJAX应用之草稿自动保存
2007-08-23 08:29:00
python爬虫中get和post方法介绍以及cookie作用
2021-06-20 06:49:00
mysql如何通过my.ini更改默认字符集
2024-01-28 08:25:07
Python实现桶排序与快速排序算法结合应用示例
2022-10-26 23:33:39
vue-cli-service build 环境设置方式
2024-05-25 15:17:08
Access函数大全
2009-12-23 19:22:00
pandas如何将datetime64[ns]转为字符串日期
2022-06-28 13:48:37
python实现canny边缘检测
2022-03-05 00:24:08
关于reduce的介绍及用法说明
2024-04-18 10:49:34
python+selenium行为链登录12306(滑动验证码滑块)
2023-01-16 16:08:01
Windows下将Python文件打包成.EXE可执行文件的方法
2021-08-04 02:47:59
Python2和Python3的共存和切换使用
2022-12-26 13:05:20
低版本中Python除法运算小技巧
2021-11-14 00:54:00
python open读取文件内容时的mode模式解析
2022-02-05 20:03:35
对python:threading.Thread类的使用方法详解
2022-01-24 04:19:28
JS数组方法汇总
2009-08-03 14:06:00