把图片列表合成一个GIF动画图片


import os
from PIL import Image
import images2gif

#type 合成GIF分类
#0:图片缩放到最大宽度*最大高度(长方形)、并粘贴到最大宽度*最大高度(长方形)的白色背景图片中、居中后合成
#1:图片缩放到最大长度(正方形)、并粘贴到最大长度(正方形)的白色背景图片中、居中后合成
#2:图片不缩放、并粘贴到最大宽度*最大高度(长方形)的白色背景图片中、居中后合成
#3:图片不缩放、并粘贴到最大长度(正方形)的白色背景图片中、居中后合成
#4:原图直接合成(按宽度排序、不缩放也不粘贴到新的白色背景图片中)
#5:原图直接合成(按高度排序、不缩放也不粘贴到新的白色背景图片中)
def GetGifAnimationFromImages(targetGifFilePath, srcImageFilePaths, type = 0):
#用来合成的图片
images = []

#取得所有图片中最大长度(宽度、高度)
maxWidthAndHeight = 1
#最大宽度和高度
maxWidth = 1
maxHeight = 1
#取得图片按宽度从大到小排序的路径顺序
widthAndFilePaths = []
#取得图片按高度从大到小排序的路径顺序
heightAndFilePaths = []

for imageFilePath in srcImageFilePaths:
fp = open(imageFilePath, "rb")
width,height = Image.open(fp).size
widthAndFilePaths.append((width, imageFilePath))
heightAndFilePaths.append((height, imageFilePath))
maxWidth = max(maxWidth, width)
maxHeight = max(maxHeight, height)
fp.close()

maxWidthAndHeight = max(maxWidthAndHeight, maxWidth, maxHeight)

#降序排列
widthAndFilePaths.sort(key=lambda item: item[0], reverse=True)
heightAndFilePaths.sort(key=lambda item: item[0], reverse=True)

if type == 4 or type == 5:
#原图直接合成(按宽度排序)
if type == 4:
for widthAndFilePath in widthAndFilePaths:
img = Image.open(widthAndFilePath[1])
images.append(img)
#原图直接合成(按高度排序)
if type == 5:
for heightAndFilePath in heightAndFilePaths:
img = Image.open(heightAndFilePath[1])
images.append(img)
else:
for imageFilePath in srcImageFilePaths:
fp = open(imageFilePath, "rb")
img = Image.open(fp)
width,height = img.size
#生成空的白色背景图片
if type == 0 or type == 2:
#长方形
imgResizeAndCenter = Image.new("RGB", [maxWidth,maxHeight], (255,255,255))
elif type == 1 or type == 3:
#正方形
imgResizeAndCenter = Image.new("RGB", [maxWidthAndHeight,maxWidthAndHeight], (255,255,255))

if type == 0:
#宽度/最大宽度>=高度/最大高度,使用小的缩放比例
if maxWidth / width >= maxHeight / height:
resizeImg = img.resize((width * maxHeight / height, maxHeight),Image.ANTIALIAS)
imgResizeAndCenter.paste(resizeImg, ((maxWidth - width * maxHeight / height)/ 2,0))
else:
resizeImg = img.resize((maxWidth, height * maxWidth / width),Image.ANTIALIAS)
imgResizeAndCenter.paste(resizeImg, (0,(maxHeight - height * maxWidth / width)/ 2))
if type == 1:
#宽度>=高度,按宽度缩放到最大长度
if width >= height:
resizeImg = img.resize((maxWidthAndHeight, height * maxWidthAndHeight / width),Image.ANTIALIAS)
imgResizeAndCenter.paste(resizeImg, (0,(maxWidthAndHeight - height * maxWidthAndHeight / width)/ 2))
else:
resizeImg = img.resize((width * maxWidthAndHeight / height, maxWidthAndHeight),Image.ANTIALIAS)
imgResizeAndCenter.paste(resizeImg, ((maxWidthAndHeight - width * maxWidthAndHeight / height)/ 2, 0))
elif type == 2:
imgResizeAndCenter.paste(img, ((maxWidth - width) / 2,(maxHeight - height) / 2))
elif type == 3:
imgResizeAndCenter.paste(img, ((maxWidthAndHeight - width) / 2,(maxWidthAndHeight - height) / 2))

# #保存缩放居中后的图片
# imgResizeAndCenter.convert("RGB").save(os.path.dirname(imageFilePath) + os.sep + "ResizeAndCenter" + os.path.basename(imageFilePath), 'jpeg')
images.append(imgResizeAndCenter)
fp.close()

images2gif.writeGif(targetGifFilePath, images, duration=1, nq=0.1)

#取得目录下面的文件列表
def GetDirImageList(dir_proc, recusive = True):
resultList = []
for file in os.listdir(dir_proc):
if os.path.isdir(os.path.join(dir_proc, file)):
if (recusive):
resultList.append(GetDirImageList(os.path.join(dir_proc, file), recusive))
continue

resultList.append(os.path.join(dir_proc, file))

return resultList

if __name__ == "__main__":
GetGifAnimationFromImages(r"D:\hecheng.gif", [r"D:\a.jpg", r"D:\b.jpg", r"D:\c.jpg"])
GetGifAnimationFromImages(r"D:\hecheng1.gif", [r"D:\a.jpg", r"D:\b.jpg", r"D:\b.jpg", r"D:\c.jpg"], 1)
GetGifAnimationFromImages(r"D:\hecheng2.gif", [r"D:\a.jpg", r"D:\b.jpg", r"D:\c.jpg"], 2)
GetGifAnimationFromImages(r"D:\hecheng3.gif", [r"D:\a.jpg", r"D:\b.jpg", r"D:\c.jpg"], 3)
GetGifAnimationFromImages(r"D:\hecheng4.gif", [r"D:\a.jpg", r"D:\b.jpg", r"D:\c.jpg"], 4)
GetGifAnimationFromImages(r"D:\hecheng5.gif", [r"D:\a.jpg", r"D:\b.jpg", r"D:\c.jpg"], 5)

GetGifAnimationFromImages(r"D:\hechengTest.gif", GetDirImageList(r"D:\GifMarker"), type = 4)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值