包体积优化-Python筛选项目中的大图

在包体积中,较大的图片资源会严重影响包的体积,可以通过以下方式进行大图的筛选,进行压缩或者放置远端处理.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# #coding:utf-8
import glob, os
# import subprocess
# import commands

filesList = []
unsupports = []
# rootPath = '/Users/jinying.zhou.o/Desktop/TestIconOC'
#rootPath = '/Users/jinying.zhou.o/Documents/NIO/FDAppKit'
rootPath = '/Users/jinying.zhou.o/Documents/NIO/NIO/NextevCar'

def get_FileSize(filePath):
fsize = os.path.getsize(filePath)
fsize = fsize/float(1024)
return round(fsize,2)

#记录所有的图片列表
filesList = []
#记录大图的列表
bigSizeList = []
#记录所有的图片总体积
totalSize = 0.0
#记录大图的size
bigTotalSize = 0.0
for root, dirs, files in os.walk(rootPath):
for file in files:
if file.endswith(".png"):
tempPath = root + '/' + os.path.join(file)
tempSize = get_FileSize(tempPath)
totalSize += tempSize
tempStr = tempPath + ' ' + str(tempSize) + "KB"
#print(tempStr)
if tempSize > 100:
bigSizePath = tempPath + ' ' + str(tempSize) + 'KB'
bigTotalSize += tempSize
bigSizeList.append(bigSizePath)

#print filesList
print("=================================以下为大图===============================")
for bigPath in bigSizeList:
print(bigPath)
print("项目中图片总大小为"+ str(totalSize) + 'KB')
print("超过100K的图片大小总size为" + str(bigTotalSize) + "KB")