在Python中使用pngquant壓縮png圖片的教程

來源:互聯網
上載者:User
說到png圖片壓縮,可能很多人知道TinyPNG這個網站。但PS外掛程式要錢(雖然有破解的),Developer API要連到他伺服器去,不提網路傳輸速度,Key也是有每月限制的。

但是貌似tinyPNG是使用了來自於 pngquant 的技術,至少在 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/)是跨平台並且開源的,個人覺得已經相當好用了,直接把檔案夾往裡面拽就能遞迴處理,支援各種形式的產生方式(改名、覆蓋、儲存到其他目錄等),壓縮結束給出壓縮比,並且還支援預覽。

但我還是會希望能夠通過指令碼來處理,一方面可定製性更強,一方面更方便整合到整個自動化的流程鏈中。於是我又拿出了python試圖寫點什麼,誰知道……

pngquant的命令列方式略坑……help中的參數說明和實際效果不一致,已經發現的問題有

1. --force 參數無效,只要輸出檔案存在,就會報錯,無視這個本用來指定覆寫的參數
2. --skip-if-larger 參數不正常,有時候組建檔案明明比較小,也會被skip掉……

不過好在python大法好,這些問題雖然命令列本身不能處理,但python可以在上層處理掉,下面就是目前實際使用的遞迴處理某檔案夾png的指令碼:

'''pngquant.pyuse pngquant to reduces png file sizeRuoqian, Chen ----------2015/4/31. 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/21. desDir can be the same to srcDir, or another dir2. lod ini config can be not exist----------2015/3/31create'''import osimport os.pathimport sysimport ConfigParserimport stringPngquantExe="pngquant"thisFilePath = sys.path[0];print "this py file in dir : " + thisFilePathprojectPath = 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 keepOrgPathssrcResPath = projectPath + srcResDir;pngCount = 0;transCount = 0;#pngquant --force --skip-if-larger --ext .png --quality 50-90 --speed 1for 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)
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.