在Python中使用pngquant壓縮png圖片的教程
說到png圖片壓縮,可能很多人知道TinyPNG這個網(wǎng)站。但PS插件要錢(雖然有破解的),Developer API要連到他服務(wù)器去,不提網(wǎng)絡(luò)傳輸速度,Key也是有每月限制的。
但是貌似tinyPNG是使用了來自于 pngquant 的技術(shù),至少在 http://pngquant.org/ 中是如此聲稱的:TinyPNG and Kraken.io — on-line interfaces for pngquant。如果真是這樣,我很想對TinyPNG說呵呵。后者是開源的,連首頁中提供的GUI工具也都是開源的。并且TinyPNG在首頁的原理說明里面,一次都沒提到pngquant
我取了tinyPNG的首頁上的示例圖用pngquant命令行跑了一下,壓縮率和顯示效果差不多。
pngquant首頁上提供的工具中,Pngyu(http://nukesaq88.github.io/Pngyu/)是跨平臺并且開源的,個人覺得已經(jīng)相當(dāng)好用了,直接把文件夾往里面拽就能遞歸處理,支持各種形式的生成方式(改名、覆蓋、存儲到其他目錄等),壓縮結(jié)束給出壓縮比,并且還支持預(yù)覽。
但我還是會希望能夠通過腳本來處理,一方面可定制性更強(qiáng),一方面更方便整合到整個自動化的流程鏈中。于是我又拿出了python試圖寫點什么,誰知道……
pngquant的命令行方式略坑……h(huán)elp中的參數(shù)說明和實際效果不一致,已經(jīng)發(fā)現(xiàn)的問題有
1. --force 參數(shù)無效,只要輸出文件存在,就會報錯,無視這個本用來指定覆寫的參數(shù)
2. --skip-if-larger 參數(shù)不正常,有時候生成文件明明比較小,也會被skip掉……
不過好在python大法好,這些問題雖然命令行本身不能處理,但python可以在上層處理掉,下面就是目前實際使用的遞歸處理某文件夾png的腳本:
'''
pngquant.py
use pngquant to reduces png file size
Ruoqian, Chen<piao.polar@gmail.com>
----------
2015/4/3
1. del option --quality=50-90, special pic need skip can config in lod ini
lod ini format:
[PixelFormat]
map_01.png=0
0 means skip in file
----------
2015/4/2
1. desDir can be the same to srcDir, or another dir
2. lod ini config can be not exist
----------
2015/3/31
create
'''
import os
import os.path
import sys
import ConfigParser
import string
PngquantExe="pngquant"
thisFilePath = sys.path[0];
print "this py file in dir : " + thisFilePath
projectPath = thisFilePath + "/../CMWar_2dx/CMWar_2dx/";
srcResDir = "Resources/";
dstResDir = "Resources/";
lodIniPath = projectPath + srcResDir + "ini/pic.ini"
keepOrgPaths = [];
if os.path.exists(lodIniPath):
config = ConfigParser.SafeConfigParser()
config.read(lodIniPath)
section = "PixelFormat";
options = config.options(section)
for option in options:
value = string.atoi(config.get(section, option))
if not value:
keepOrgPaths.append(option);
print keepOrgPaths
srcResPath = projectPath + srcResDir;
pngCount = 0;
transCount = 0;
#pngquant --force --skip-if-larger --ext .png --quality 50-90 --speed 1
for parent,dirnames,filenames in os.walk(srcResPath):
print "----- process Dir " + parent
dstDir = parent.replace(srcResDir, dstResDir)
if not os.path.exists(dstDir):
os.makedirs(dstDir)
for filename in filenames:
if os.path.splitext(filename)[1] == '.png':
pngCount += 1;
srcFilePath = os.path.join(parent, filename);
dstFilePath = os.path.join(dstDir, filename);
tmpFilePath = dstFilePath + ".tmp";
if filename in keepOrgPaths:
print "----- keep ----- " + filename;
else:
# print "----- process ----- " + filename;
# cmd = "\"" + PngquantExe + "\"" + " --force --speed=1 --quality=50-90 -v " + srcFilePath + " -o " + tmpFilePath;
cmd = "\"" + PngquantExe + "\"" + " --force --speed=1 " + srcFilePath + " -o " + tmpFilePath;
# print cmd;
os.system(cmd)
if os.path.exists(tmpFilePath):
sizeNew = os.path.getsize(tmpFilePath);
sizeOld = os.path.getsize(srcFilePath);
if sizeNew < sizeOld:
open(dstFilePath, "wb").write(open(tmpFilePath, "rb").read())
transCount += 1;
os.remove(tmpFilePath)
if not os.path.exists(dstFilePath):
open(dstFilePath, "wb").write(open(srcFilePath, "rb").read())
print "Done. Trans Pngs: %d/%d" %(transCount, pngCount)
相關(guān)文章
pytorch 如何把圖像數(shù)據(jù)集進(jìn)行劃分成train,test和val
這篇文章主要介紹了pytorch 把圖像數(shù)據(jù)集進(jìn)行劃分成train,test和val的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
python高階函數(shù)map()和reduce()實例解析
這篇文章主要介紹了python高階函數(shù)map()和reduce()實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析
這篇文章主要介紹了Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
linux下python使用sendmail發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了linux下python使用sendmail發(fā)送郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05

