iOSプロジェクトダイエットロット圧縮プロジェクト内の画像リソース
1414 ワード
プロジェクト内のピクチャリソースを一括圧縮
IMG_3552.jpg
1.pipのインストール:sudo easy_install pip
2.TinyPngライブラリのインストール:sudo pip install--upgrade tinify
4.TinyPNG AppKeyの申請
5.Pythonスクリプトの一括圧縮Imagesを実行する.xcassetsディレクトリの下の画像
Demo: Images.xcassets/Launchディレクトリの下の画像を一括圧縮する
効果:圧縮前1.3 MB圧縮後381 KB
スクリプトアドレス
import tinify
import os
import os.path
import shutil
tinify.key = "xxx"
fromFilePath = "xxx/Images.xcassets"
toFilePath = "xxx"
for root, dirs, files in os.walk(fromFilePath):
for name in files:
fileName, fileSuffix = os.path.splitext(name)
toFullPath = toFilePath + root[len(fromFilePath):]
toFullName = toFullPath + '/' + name
if not os.path.exists(toFullPath):
os.makedirs(toFullPath)
if fileSuffix == '.png' or fileSuffix == '.jpg':
source = tinify.from_file(root + '/' + name)
source.to_file(toFullName)
with open(toFullName, 'rb') as source:
source_data = source.read()
result_data = tinify.from_buffer(source_data).to_buffer()
else:
if fileSuffix == '.json':
shutil.copy2(root + '/' + name, toFullName)
else:
print ("copied failed %s"%(root + '/' + name))