I'm trying to put a non-widescreen video frame image into a 16:9 black frame, for example: if my project is set to 720p (1280 * 720) and the size of my video frame is 640 * 480, first I calculate the zoom ratio, resize its height to 720 (and the width correspondingly) then I put it on a black 1280 * 720 image.
Tried this method but it doesn't work for me for some reason.
import cv2
import numpy as np
project_width, project_height = 1280, 720
video_file = "any_path/whatever.mp4"
black_image = np.zeros((project_height,project_width,3), np.uint8)
vidcap = cv2.VideoCapture(video_file)
while(vidcap.isOpened()):
ret, frame = vidcap.read()
if ret==True:
frame_height, frame_width, channels = frame.shape
if frame_height < project_height or frame_width < project_width:
zoom_ratio = project_height/frame_height
desired_width = int(frame_width * zoom_ratio)
desired_height = project_height
frame = cv2.resize(frame, (desired_width, desired_height))
border_width = int((project_width-desired_width) / 2)
black_image[0:, border_width:] = frame
At this point I got an error:
TypeError: slice indices must be integers or None or have an index method
EDIT: my bad, I forgot to convert border_width to an int, but now I face another problem:
ValueError: could not broadcast input array from shape (720,1152,3) into shape (720,1216,3)
(my source video is 1152*720 in this actual case)
EDIT2 - I suddenly realized the solution: on the last line I must specify the end pixel of the image so it works. In case of someone has this problem, too:
black_image[0:, border_width:project_width - border_width] = frame