python3 webp转gif格式的实现示例

yipeiwu_com7年前Python基础

使用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设计】。

相关文章

pandas的相关系数与协方差实例

1、输出百分比变化以及前后指定的行数 a = np.arange(1,13).reshape(6,2) data = DataFrame(a) #计算列的百分比变化,如果...

pytorch 自定义数据集加载方法

pytorch 官网给出的例子中都是使用了已经定义好的特殊数据集接口来加载数据,而且其使用的数据都是官方给出的数据。如果我们有自己收集的数据集,如何用来训练网络呢?此时需要我们自己定义好...

Python使用pymysql小技巧

在使用pymysql的时候,通过fetchall()或fetchone()可以获得查询结果,但这个返回数据是不包含字段信息的(不如php方便)。查阅pymysql源代码后,其实获取查询结...

Python遍历目录的4种方法实例介绍

1.os.popen运行shell列表命令 复制代码 代码如下: def traverseDirByShell(path):     for f in os...

Python实现正则表达式匹配任意的邮箱方法

Python实现正则表达式匹配任意的邮箱方法

首先来个简单的例子,利用Python实现匹配163邮箱的代码: #-*- coding:utf-8 -*- __author__ = '杨鑫' import re text = in...