Python-29 用PIL(pillow)批量更改图片大小并用moviepy制作成视频 2020-07-29

1、图片大小会统一,视频也制成,结果如下图:


image.png

2、具体实现与代码解释如下:
注意:做视频时需要图片大小一致,所以用PIL先对图片大小统一化!

#1 convert images into same size, bcoz in step2 need same size for video
from PIL import Image #pip install pillow
import os.path
import glob
def convertjpg(jpgfile,outdir,width=128,height=128):
  img=Image.open(jpgfile)
  try:
    new_img=img.resize((width,height),Image.BILINEAR)
    new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
  except Exception as e:
    print(e)
for jpgfile in glob.glob("C:\\Users\\Mr.R\\Pictures\\wallpaper\\*.jpg"):
  convertjpg(jpgfile,"C:/Users/Mr.R/Pictures/wallpaper1")

#2 make the vedio 
import os
import moviepy.video.io.ImageSequenceClip #pip install moviepy
image_folder='C:/Users/Mr.R/Pictures/wallpaper1'#open the image location
fps=1

image_files = [image_folder+'/'+img for img in os.listdir(image_folder) if img.endswith(".jpg")]
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)
clip.write_videofile('C:/Users/Mr.R/Pictures/wallpaper1/Movie1.mp4')

你可能感兴趣的:(Python-29 用PIL(pillow)批量更改图片大小并用moviepy制作成视频 2020-07-29)