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()
这是我的代码,通过改变图像的边界来创建一个视频,它的工作和输出的视频文件,但视频文件不打开,在我的PC 当我尝试播放视频时,它是错误的压缩代码四cc,我试过不同的四cc "xvid","mpv4","mpjg "没有工作了。
你的输出视频帧大小不正确。你的输入帧尺寸是(1080,600)。我们在每边添加了40px的边框,所以最终的帧尺寸将是(1080+40*2,600+40*2),即(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()