本帖最后由 Ag2S 于 2020-2-16 10:07 编辑
PS:pngquant:https://pngquant.org/pngquant-windows.zip
[Python] 纯文本查看 复制代码 #!/usr/bin/python3
# coding=utf-8
import os, sys
import re
#调用pngquant压缩png
def tinypng(img):
path='C:/libs/ffmpeg/pngquant/pngquant.exe'
#path='pngquant.exe' #如果配置了环境变量
cmd=path+' --force --skip-if-larger --verbose 256 \"'+img+'\"'
res=os.popen(cmd);
output_str = res.read()
print(output_str)
def tinAllPNG(rootdir):
_files = []
list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isdir(path):
_files.extend(tinAllPNG(path))
if os.path.isfile(path):
if(path.endswith('.png')and path.endswith('-fs8.png')==False):
tinypng(path)
print(path)
_files.append(path)
#print(_files)
return _files
def ReplaceAll(rootdir):
_files = []
list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isdir(path):
_files.extend(ReplaceAll(path))
if os.path.isfile(path):
if(path.endswith('-fs8.png')):
npath=path.replace('-fs8','')
os.remove(npath)
os.rename(path,npath)
print(path)
_files.append(path)
#print(_files)
return _files
tinAllPNG('./')
ReplaceAll('./')
|