1pip install Pillow
1'''导入依赖包'''
2
3# 系统操作库
4
5import os
6
7# 图片处理库
8
9from PIL import Image
1# 初始化图片地址文件夹途径
2
3image_path = '/usr/load/picture'
4
5# 获取文件列表
6
7files = os.listdir(image_path)
8
9# 定义第一个文件的全局路径
10
11file_first_path = os.path.join(image_path, files[0])
12
13# 获取Image对象
14
15img = Image.open(file_first_path)
16
17# 初始化文件对象数组
18
19images = []
20
21for image in files[1:]:
22
23 # 获取当前图片全量路径
24
25 img_path = os.path.join(image_path, image)
26
27 # 将当前图片使用Image对象打开、然后加入到images数组
28
29 images.append(Image.open(img_path))
30
31# 保存并生成gif动图
32
33img.save('beauty.gif', save_all=True, append_images=images, loop=0, duration=2000)
联系客服