In this tutorial, I will show how to change the resolution of the video using OpenCV-Python. This blog is based on interpolation methods (Chapter-5) which we have discussed earlier.
Here, I will convert a 640×480 video to 1280×720. Let’s see how to do this
Steps:
- Load a video using cv2.VideoCapture()
- Create a VideoWriter object using cv2.VideoWriter()
- Extract frame by frame
- Resize the frames using cv2.resize()
- Save the frames to a video file using cv2.VideoWriter()
- Release the VideoWriter and destroy all windows
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import cv2 import numpy as np cap = cv2.VideoCapture('C:/New folder/video.avi') fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 5, (1280,720)) while True: ret, frame = cap.read() if ret == True: b = cv2.resize(frame,(1280,720),fx=0,fy=0, interpolation = cv2.INTER_CUBIC) out.write(b) else: break cap.release() out.release() cv2.destroyAllWindows() |
Here, I have used Bicubic as the interpolation method, you can use any. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.
the code was really cool dude but i need the code in minimum lines ,if it is possible pls do post the answer
Great example but why did you import numpy? and not using?
Thanks for the code. The resolution is changing but my video gets rotated pls help