网上有很多程序示例描述了如何在python中将多张图片合成为视频,但好像都没有关注一个小问题——被合成的那些图片的尺寸应该一致,并且和程序中给定的参数一致。我遇到的问题就是,如果多张图片的尺寸不一致,或者如果图片的尺寸与给定的参数不一致,则生成出来的视频会有问题,无法播放。针对这个问题,有必要对多张图片的尺寸进行统一,即通过下面的resize函数实现。
import cv2
import glob
def resize(img_array, align_mode):
_height = len(img_array[0])
_width = len(img_array[0][0])
for i in range(1, len(img_array)):
img = img_array[i]
height = len(img)
width = len(img[0])
if align_mode == 'smallest':
if height < _height:
_height = height
if width < _width:
_width = width
else:
if height > _height:
_height = height
if width > _width:
_width = width
for i in range(0, len(img_array)):
img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
img_array[i] = img1
return img_array, (_width, _height)
def images_to_video(path):
img_array = []
for filename in glob.glob(path+'/*.png'):
img = cv2.imread(filename)
if img is None:
print(filename + " is error!")
continue
img_array.append(img)
# 图片的大小需要一致
img_array, size = resize(img_array, 'largest')
fps = 1
out = cv2.VideoWriter('demo.avi', cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
def main():
path = "../pictures/test/"
images_to_video(path)
if __name__ == "__main__":
main()