目的
應(yīng)用開(kāi)發(fā)中某残,安裝包的大小是很重要的,所以需要對(duì)資源進(jìn)行壓縮怀伦,特別是圖片。這是一個(gè)python編寫(xiě)的簡(jiǎn)單的.png批量壓縮工具凡恍。
環(huán)境配置
python 環(huán)境配置
自然需要 python志秃,見(jiàn)官網(wǎng) https://www.python.org/下載
壓縮API配置
亦需圖片壓縮 API,我們使用的是這個(gè):https://tinypng.com 嚼酝。
第一步:進(jìn)入https://tinypng.com/developers浮还,注冊(cè)一個(gè) Developer API Key,免費(fèi)用戶(hù)一個(gè)月有500張圖的限制闽巩,付費(fèi)則各有不同钧舌,請(qǐng)按需自取。
第二步:下載API對(duì)應(yīng)的lib涎跨。參閱官網(wǎng):https://tinypng.com/developers/reference/python 也有其他語(yǔ)言API洼冻。
終端命令:
sudo -H pip install —upgrade tinify
找到下載目錄,執(zhí)行命令:
sudo python setup.py
如需先安裝 pip隅很,終端命令:
easy_install pip
也可以直接下載壓縮包 https://pypi.python.org/pypi/tinify 或者訪問(wèn) github https://github.com/tinify/tinify-python撞牢,將 build/lib 中的 tinify 目錄拷貝至 Python lib 目錄下:
我的是 /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4
PS:就算不使用這個(gè)API,僅僅這個(gè)主頁(yè)提供的圖片壓縮服務(wù)也是極好的叔营。
使用向?qū)?br> 填寫(xiě)已申請(qǐng)好的 API_KEY 至 tinify.key="your_key"屋彪,將腳本拷貝至需壓縮圖片的文件夾下,即會(huì)批量壓縮該文件夾中所有大小大于 1KB 的 .png 圖片绒尊。
源碼(忽略第一行):
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ba2da2}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s3 {font-variant-ligatures: no-common-ligatures; color: #ba2da2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s5 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
import os
from tinify import tinify
from os import path
def get_file_name():
# Initialize
max_size = 1024
compressed_size = 100
dic_path = os.getcwd()
f_list = os.listdir(os.getcwd())
out_list = {}
# Read compress history
index = 0
f_history = open("history_compressed.txt", 'a+')
f_history.seek(0)
f_history_pics = {}
for line in f_history:
f_history_pics[index] = line.strip('\n')
index += 1
# do for all files under current directory
index = 0
for i in f_list:
file = dic_path + "/" + os.path.basename(i)
buffer_file = path.splitext(file)[0] + "Copy" + path.splitext(file)[1]
# compress all .pngs whitch size > max_size and not in compress history
if not contains(file, f_history_pics) and path.splitext(file)[1] == '.png' and path.getsize(file) > max_size:
f_history.write(file + "\n")
# use API from tinypng.com to compress pictures
tinify.from_file(file).to_file(buffer_file)
# replace the origin file with buffer file when their size difference is bigger than compressed_size
if (path.getsize(file) - path.getsize(buffer_file) < compressed_size):
os.remove(buffer_file)
else:
os.rename(buffer_file, file)
print(file)
index += 1
f_history.flush()
f_history.close()
return out_list
def contains(name, name_his={}):
for i in range(0, len(name_his)):
if name == name_his[i]:
return True
return False
tinify.key = "YOUR_KEY"
get_file_name()
print("finish!")