1
import numpy
import cv2
import random
image=cv2.imread(r"C:\Users\Sriram\Desktop/img.png")
print(image.shape)
image=cv2.resize(image,(1080,600))
fourcc=cv2.VideoWriter_fourcc(*"MP4V")
def rand():
    n=random.randrange(0,255,15)
    x=random.randrange(0,255,15)
    y=random.randrange(0,255,15)

    return cv2.copyMakeBorder(image,40,40,40,40,cv2.BORDER_CONSTANT,None,(x,n,y))
#cv2.imwrite(r"C:\Users\Sriram\Desktop/new.jpg",image)
l=[rand() for i in range(30)]
print(l)
video=cv2.VideoWriter(r"C:\Users\Sriram\Desktop/video.mp4",fourcc,25,(1080,600))
for i in l:
    video.write(i)

cv2.destroyAllWindows()
video.release()

this is my code to create a video from image just by changing border of images and it worked and outputs a video file.but the video file does not open in my pc when i try to play video it syas incorrect compression code fourcc i tried different fourcc "xvid","mpv4","mpjg"nothing worked out

1 Answer 1

2

Your output video frame size was not correct. Your input frame size was (1080,600). we were adding 40px border on each side so the final frame dimension will be (1080+40*2,600+40*2) which is (1160,680)

import numpy
import cv2
import random
image=cv2.imread(r"C:\Users\Sriram\Desktop/img.png")
print(image.shape)
image=cv2.resize(image,(1080,600))
fourcc=cv2.VideoWriter_fourcc(*"MP4V")
def rand():
    n=random.randrange(0,255,15)
    x=random.randrange(0,255,15)
    y=random.randrange(0,255,15)

    return cv2.copyMakeBorder(image,40,40,40,40,cv2.BORDER_CONSTANT,None,(x,n,y))
#cv2.imwrite(r"C:\Users\Sriram\Desktop/new.jpg",image)
l=[rand() for i in range(30)]
print(l)
video=cv2.VideoWriter(r"C:\Users\Sriram\Desktop/video.mp4",fourcc,25,(1160,680))
for i in l:
    video.write(i)

cv2.destroyAllWindows()
video.release()

Not the answer you're looking for? Browse other questions tagged or ask your own question.