How to change video resolution in OpenCV in Python
Last Updated :
03 Jan, 2023
In this article, we will describe how you can capture each frame from the video and also resize them according to your needs. What we will exactly do is take the video as input from the user and capture it frame by frame. Moreover, we run the video in the loop and save the image frames with certain names after resizing those frames.
Stepwise Implementation:
Step 1: Importing the libraries
Here, we are importing the cv2 library, the cv2 is the OpenCV package that helps us to call the imread(), startWindowThread(), namedWindow(), and imshow() functions respectively.
import cv2
Step 2: Define a video capture object
In this step, we will use the function VideoCapture() to get a video capture object for the camera. Here, you can use either your camera or directly upload the video from your system.
vidcap = cv2.VideoCapture("#Define the video path")
Step 3: Capture video frame by frame
Now, we will capture the length of the video for running the loop. Also, the video gets automatically closed once all the frames have been captured.
success,image = vidcap.read()
Step 4: Declare the variableĀ
Then, declare the variable, and count with the value 0. This variable will further be incremented while running the loop.
count = 0
Step 5: Creating a loop for running the video
Moreover, we create a while loop for running the video and saving all the frames from your video into your local system.Ā
while success:
Step 5.1: Capture video frame by frame
Next, we will read the video frame by frame for resizing the frames and saving them to your local computer.
success,image = vidcap.read()
Step 5.2: Resizing the image frames
In this step, we have used resize() function with particular dimensions for which the image frames need to be set.
Ā resize = cv2.resize(image, (#x-axis dimensions, #y-axis dimensions))
Step 5.3: Saving the frames with certain names
Later on, we save the image frames we resize in the last step. Also, we give the frames certain names with the extensions.
Ā cv2.imwrite("%04d.jpg" % count, resize)
Step 5.4: Ā Closing the video automatically once it gets over
Further, the waitKey() function is used for closing the video automatically once the video gets over.
if cv2.waitKey(10) == 27:
break
Step 5.5 Incrementing the variable by 1
Finally, we increment the value of the variable declared, count by 1.
count += 1
Example:
In this example, we have used the video (link) from which we will extract certain frames, resize those frames and save them with the particular names on the local computer.
Python3
# Python program to resize frame's
# from video with aspect ratio
# Import the libraries
import cv2
# Define a video capture object
vidcap = cv2.VideoCapture("gfg_video.mp4")
# Capture video frame by frame
success, image = vidcap.read()
# Declare the variable with value 0
count = 0
# Creating a loop for running the video
# and saving all the frames
while success:
# Capture video frame by frame
success, image = vidcap.read()
# Resize the image frames
resize = cv2.resize(image, (700, 500))
# Saving the frames with certain names
cv2.imwrite("%04d.jpg" % count, resize)
# Closing the video by Escape button
if cv2.waitKey(10) == 27:
break
# Incrementing the variable value by 1
count += 1
Output:
Similar Reads
How to Check OpenCV Version in Python
OpenCV (Open Source Computer Vision Library) is a powerful library for computer vision and image processing tasks. Whether you're a beginner or an experienced developer, knowing how to check the version of OpenCV you're using can be essential for compatibility and troubleshooting. In this article, w
3 min read
Change image resolution using Pillow in Python
Prerequisites: Python pillow PIL is the Python Imaging Library which provides the python interpreter with an in-depth file format support, an efficient internal representation, and fairly powerful image processing capabilities. Changing the resolution of an image simply means reducing or increasing
2 min read
Creating a Slow Motion Video Using OpenCV - Python
In this article, we will try to create a slow-motion video using OpenCV( Open Source Computer Vision) library in Python. OpenCV ( is an open-source computer vision and machine learning software library. Now a video is basically a set of moving pictures moving at a rate of about 24 frames per second
4 min read
Image Thresholding in Python OpenCV
Image Thresholding is an intensity transformation function in which the values of pixels below a particular threshold are reduced, and the values above that threshold are boosted. Â This generally results in a bilevel image at the end, where the image is composed of black and white pixels. Thresholdi
2 min read
How to Fix: VideoCapture can't open camera by index in Python
Most computer vision programs or programs that include openCV codes require a camera to capture and process the videos or images. It may happen that we get different types of errors while running this script. In this article, we are going to look at how to solve this camera-not-fed issue. In OpenCV,
3 min read
How to Install OpenCV for Python on Windows?
Prerequisite: Python Language Introduction  OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in todayâs systems. By using it, one can process images and videos to identify
4 min read
Display date and time in videos using OpenCV - Python
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. It can process images and videos to identify objects, faces, or even the handwriting of a humanNote: For more information, refer to Introduction to OpenCVÂ Display date and time in videos It sometimes becomes n
2 min read
Python | Play a video in reverse mode using OpenCV
OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV's application areas include : 1) Facial recognition system 2) motion tracking 3) Artificial neural network 4) Deep neural network 5) video streaming etc
3 min read
Click response on video output using Events in OpenCV â Python
OpenCV is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. In this article, we will create a program with a click response on video output using events in OpenCV Python library.
3 min read
Get video duration using Python - OpenCV
OpenCV is one of the most popular cross-platform libraries and it is widely used in Deep Learning, image processing, video capturing, and many more. In this article, we will learn how to get the duration of a given video using python and computer vision. Prerequisites: Opencv moduledatetime module
1 min read