使用PIL库,python3安装需要使用
pip install pillow
from PIL import Image
import os
import re
imgPath = './img/'
webpReg = r'webp$'
def getAllFile (filePath):
files = os.listdir(filePath)
return files
def handleConversion (fileName):
fileName = imgPath + fileName
print('fileName--->', fileName)
saveName = fileName.replace('webp', 'gif')
print('saveName--->', saveName)
im = Image.open(fileName)
im.save(saveName, 'gif', save_all=True)
def main():
files = getAllFile('./img')
for fileName in files:
gifName = fileName.replace('.webp', '.gif')
# webp格式且未转过的才需要转
if re.findall(webpReg, fileName) != [] and (gifName not in files):
handleConversion(fileName)
if __name__ == '__main__':
main()
图片文件放在img目录下
您可能感兴趣的文章:基于Python实现拆分和合并GIF动态图python 含子图的gif生成时内存溢出的方法Python图像处理之gif动态图的解析与合成操作详解利用Python如何制作好玩的GIF动图详解用Python将动态GIF图片倒放播放的方法