利用python3统一将网站图片转换为webp格式
为了统一将图片转换为webp格式,这里写了python3脚本,统一进行转换。
#coding:utf-8
from os import walk
import os
#查找所有png jpg jpeg的图片路径
def all_path(dirname):
result = []#所有的文件
for maindir, subdir, file_name_list in os.walk(dirname):
for filename in file_name_list:
if (filename.find('.') >= 0) and (filename.find('.webp') < 0):
apath = os.path.join(maindir,filename)
result.append(apath)
print (len(result))
return result
def searchAllUnConvertFiles(imagePathArray):
result = []#所有的文件
for imagePath in imagePathArray:
webp_path = str(imagePath + ".webp")
if os.path.exists(webp_path) == False:
result.append(imagePath)
print (len(result))
return result
def converFiles(imagePathArray):
for imagePath in imagePathArray:
webp_path = str(imagePath + ".webp")
shell = str("cwebp " + imagePath + " -o " + webp_path)
os.system(shell)
print ('转换成功: %s',webp_path)
path = '/home/'
allPath = all_path(path)
UnConvertFiles = searchAllUnConvertFiles(allPath)
print (len(UnConvertFiles))
converFiles(UnConvertFiles)