


How to implement video reading and saving functions in python (code example)
This article brings you an introduction to how python implements video reading and saving functions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1.Open the camera
#打开摄像头 import cv2 cap = cv2.VideoCapture(0) while(True): ret,frame = cap.read()#返回两个值,第一个为bool类型,如果读到帧返回True,如果没读到帧返回False,第二个值为帧图像 gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) cv2.imshow('frame',gray) if cv2.waitKey(1)==27: break cap.release() cv2.destroyAllWindows()
2.Read the video file
#打开视频文件 import cv2 cap = cv2.VideoCapture('vtest.avi') while(True): ret,frame = cap.read()#返回两个值,第一个为bool类型,如果读到帧返回True,如果没读到帧返回False,第二个值为帧图像 if(ret): gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) cv2.imshow('input',gray) else: break if cv2.waitKey(1)==27: break cap.release() cv2.destroyAllWindows()
3.Save the video file
#保存视频文件 import cv2 fourcc = cv2.VideoWriter_fourcc(*'XVID')#视频编码格式 out = cv2.VideoWriter('save.avi',fourcc,20,(640,480))#第三个参数为帧率,第四个参数为每帧大小 cap = cv2.VideoCapture(0) while(True): ret,frame = cap.read() if(ret): cv2.imshow('input',frame) out.write(frame) else: break if(cv2.waitKey(1)==27): break cap.release() out.release() cv2.destroyAllWindows()
The above is the detailed content of How to implement video reading and saving functions in python (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
