本文主要是介绍经常用到的代码段,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
opencv
opencv-图像部分
opencv显示图像
cv2.namedWindow("just for test", 0)
cv2.imshow("just for test", index_img)
cv2.waitKey(0)
Opencv转PIL.Image
Image.fromarray(cv2.cvtColor(cv_img,cv2.COLOR_BGR2RGB))
PIL.Image转Opencv
cv2.cvtColor(numpy.asarray(Img_img),cv2.COLOR_RGB2BGR)
opencv-视频部分
opencv读取视频
import cv2video_stream = cv2.VideoCapture(video_path)
读取视频的相关信息
帧率
fps = int(round(cap.get(cv2.CAP_PROP_FPS)))
分辨率-宽度
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
分辨率-高度
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
总帧数
frame_counter = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))video_frames = []
while True:still_reading, frame = video_stream.read()if not still_reading:video_stream.release()breakvideo_frames.append(frame)opencv将图片写成视频
import cv2fourcc = cv2.VideoWriter_fourcc(*'mp4v')
videowriter = cv2.VideoWriter(args.video_save_path, fourcc, 25, size)
for mask_name in sorted(os.listdir(args.image_mask_folder)):mask_path = os.path.join(args.image_mask_folder, mask_name)mask = cv2.imread(mask_path)mask[mask != (0, 0, 0)] = 255mask[mask == (0, 0, 0)] = 0videowriter.write(mask)
videowriter.release()
时间time模块
输出格式化的时间
import timecur_time = time.strftime("%Y%m%d%H%M", time.localtime())
python运行程序模块
指定程序运行的根目录
export PYTHONPATH=./
subprocess执行的代码模块
video_process_cmd = ['ffmpeg','-i', input_path,'-vcodec', 'libx264', # TODO'-b:v', str(modify_bitrate), # 设置视频的码率'-bufsize', str(modify_bitrate), # 设置缓冲区大小'-maxrate', str(modify_bitrate), # 限制最大码率,确保不会超过2Mbps'-c:a', 'copy',output_path, "-y"
]
subprocess.run(video_process_cmd)
这篇关于经常用到的代码段的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!