最近抽空审查游戏代码的时候,发现部分游戏的资源居然高达80M,着实惊呆了,查看相关资源后,发现大部分资源未进行压缩处理,手动压缩各个资源,有感觉太吃力,所以自己使用python开发了一个资源压缩软件
目录结构比较简单,主要是依赖与pngquant,网上都说这个工具压缩效果还是很不错。
三个目录:pngquant(主要的应用所在目录);config.ini(用于配置想压缩的文件目录);main.py(咋们的python主程序)
#安装配置文件解析工具
pip install configparser
# 读取配置文件
con = configparser.ConfigParser()
con.read("config.ini",encoding="utf-8")
items=con.items('path')
pathOrigin = items[0][1]
pathDest = items[1][1]
pathOrigin就是咋们要进行遍历的目录,接下来对某个路径遍历所有的文件
# 遍历文件夹,如果是文件夹,在再往内部遍历
def recursive_listdir(path):
files = os.listdir(path)
for file in files:
file_path = os.path.join(path, file)
print(file_path)
if os.path.isfile(file_path):
# print(file_path)
# 取文件后缀名,方法一:.png
print(os.path.splitext(file_path)[1])
# 取文件后缀名,方法二:png
print(file_path.split('.')[1])
fileType = file_path.split('.')[1]
if fileType == "png" or fileType == "jpg" :
commpress(file_path)
elif os.path.isdir(file_path):
# print("asdfasdf",file_path)
recursive_listdir(file_path)
对各个资源进行压缩处理
# 运行系统命令行
def runSystem(ossys):
result = os.system(ossys)
if result == 0:
print(str(ossys) + ' success')
return True
else:
print(str(ossys) + ' fail')
return False
def commpress(filepath):
# im = Image.open(filepath)
# im.save(pathDest+"/abd.png",quality=5)
# print("commpress:",pathDest,filepath)
# 此方法可行,但需要再https://tinify.com 申请key(方法一)
# source = tinify.from_file(filepath)
# source.to_file(pathDest+"/abd.png")
# 此方法获取不到图片的拍摄日志等(方法二)
image_stat = os.stat(filepath)
# 计算图片的尺寸
image_size = image_stat.st_size / 1024.0
print('st_size:{0} image_size:{1}'.format(image_stat.st_size, image_size))
# 获取基本的文件名称
base_name = os.path.basename(filepath)
print("base_name:",base_name,os.getcwd())
commond = ('pngquant\\pngquant.exe --force --verbose --speed=1 --quality={2}-100 {0} -o {1}').format(filepath, filepath, 20)
runSystem(commond)
完整的代码如下:
import configparser
import os
import re
from PIL import Image
import tinify
tinify.key = 'MBzRm8W5WBV8LZVtmFXPzpmmZRwPq3Yw'
# 运行系统命令行
def runSystem(ossys):
result = os.system(ossys)
if result == 0:
print(str(ossys) + ' success')
return True
else:
print(str(ossys) + ' fail')
return False
def commpress(filepath):
# im = Image.open(filepath)
# im.save(pathDest+"/abd.png",quality=5)
# print("commpress:",pathDest,filepath)
# 此方法可行,但需要再https://tinify.com 申请key(方法一)
# source = tinify.from_file(filepath)
# source.to_file(pathDest+"/abd.png")
# 此方法获取不到图片的拍摄日志等(方法二)
image_stat = os.stat(filepath)
# 计算图片的尺寸
image_size = image_stat.st_size / 1024.0
print('st_size:{0} image_size:{1}'.format(image_stat.st_size, image_size))
# 获取基本的文件名称
base_name = os.path.basename(filepath)
print("base_name:",base_name,os.getcwd())
commond = ('pngquant\\pngquant.exe --force --verbose --speed=1 --quality={2}-100 {0} -o {1}').format(filepath, filepath, 20)
runSystem(commond)
# 遍历文件夹,如果是文件夹,在再往内部遍历
def recursive_listdir(path):
files = os.listdir(path)
for file in files:
file_path = os.path.join(path, file)
print(file_path)
if os.path.isfile(file_path):
# print(file_path)
# 取文件后缀名,方法一:.png
print(os.path.splitext(file_path)[1])
# 取文件后缀名,方法二:png
print(file_path.split('.')[1])
fileType = file_path.split('.')[1]
if fileType == "png" or fileType == "jpg" :
commpress(file_path)
elif os.path.isdir(file_path):
# print("asdfasdf",file_path)
recursive_listdir(file_path)
# 读取配置文件
con = configparser.ConfigParser()
con.read("config.ini",encoding="utf-8")
items=con.items('path')
pathOrigin = items[0][1]
pathDest = items[1][1]
# 遍历源目录下的所有文件
recursive_listdir(pathOrigin)
# #coding=utf-8
# import tinify
# import os
# import shutil
# tinify.key = "填写自己的key值"
# # tinify.proxy = "http://user:pass@192.168.0.1:8080"
# #未压缩图片路径
# uncompressResPath = "未压缩图片的图片根路径";
# #压缩的图片存放路径
# compressedResPath = "压缩完成的图片存放路径";
# #用于存放检索出的符合条件的大图
# largeRes = "存放检索出的符合条件的大图";
# #每个月能够使用的最大压缩数量
# maxMonthCompressCount = 500;
# compressions_this_month = 0;
# BITAXSIZE = 1024.0
# KBMAXSIZE = BITAXSIZE * 1024.0
# MBMAXSIZE = KBMAXSIZE * 1024.0
# GBMAXSIZE = MBMAXSIZE * 1024.0
# TBMAXSIZE = GBMAXSIZE * 1024.0
# totalSize = 0;
# #压缩单张资源
# def compressSimpleRes(resAbsolutePath,fileName):
# global compressions_this_month;
# print("start compress res");
# print("original Res Size:" + size_format(os.path.getsize(resAbsolutePath)));
# source = tinify.from_file(resAbsolutePath);
# print("upload Res success");
# source.to_file(compressedResPath +fileName);
# compressions_this_month = tinify.compression_count;
# print("left Compress Times:" + "%i"%(maxMonthCompressCount - compressions_this_month));
# print("compress res success");
# print("compressed Res Size:" + size_format(os.path.getsize(compressedResPath +fileName)))
# #压缩根目录下指定数量的png资源
# def compressAllPng(path):
# allFile = os.listdir(path);
# for eachFile in allFile:
# if os.path.isdir(path + os.sep + eachFile):
# compressAllPng(path + os.sep + eachFile);
# else:
# if(".png" in eachFile):
# if(compressions_this_month >= maxMonthCompressCount):
# return
# print("res Name:" + eachFile);
# compressSimpleRes(path + os.sep + eachFile,eachFile);
# #获得文件大小
# def size_format(size):
# if size < BITAXSIZE:
# return '%i' % size + 'size'
# elif BITAXSIZE <= size < KBMAXSIZE:
# return '%.2f' % round(size/BITAXSIZE,2)+'KB'
# elif KBMAXSIZE <= size < MBMAXSIZE:
# return '%.2f' % round(size/KBMAXSIZE,2)+'MB'
# elif MBMAXSIZE <= size < GBMAXSIZE:
# return '%.2f' % round(size/MBMAXSIZE,2)+'GB'
# elif GBMAXSIZE <= size:
# return '%.2f' % round(size/GBMAXSIZE,2)+'TB'
# #拷贝文件到目标目录
# def copyToDest(filePath):
# shutil.copy(filePath,largeRes + os.sep);
# #收集根目录下大于指定大小的png资源
# def collectLargeSizeRes(path):
# global totalSize;
# allFile = os.listdir(path);
# for eachFile in allFile:
# fileAbsolutePath = path + os.sep + eachFile;
# if os.path.isdir(fileAbsolutePath):
# collectLargeSizeRes(fileAbsolutePath);
# else:
# if(".png" in eachFile):
# fileSize = os.path.getsize(fileAbsolutePath);
# #如果文件大于1M则将此文件copy到largeRes文件夹
# if(fileSize >= KBMAXSIZE):
# totalSize = totalSize + fileSize;
# print("total size:" + size_format(totalSize));
# copyToDest(fileAbsolutePath);
# collectLargeSizeRes(uncompressResPath);
# compressAllPng(largeRes);
# print(compressions_this_month);
资源下载路径:https://download.csdn.net/download/wushi333333/88519626