在Python中使用pngquant壓縮png圖片的教程
說到png圖片壓縮,可能很多人知道TinyPNG這個(gè)網(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。如果真是這樣,我很想對(duì)TinyPNG說呵呵。后者是開源的,連首頁中提供的GUI工具也都是開源的。并且TinyPNG在首頁的原理說明里面,一次都沒提到pngquant
我取了tinyPNG的首頁上的示例圖用pngquant命令行跑了一下,壓縮率和顯示效果差不多。
pngquant首頁上提供的工具中,Pngyu(http://nukesaq88.github.io/Pngyu/)是跨平臺(tái)并且開源的,個(gè)人覺得已經(jīng)相當(dāng)好用了,直接把文件夾往里面拽就能遞歸處理,支持各種形式的生成方式(改名、覆蓋、存儲(chǔ)到其他目錄等),壓縮結(jié)束給出壓縮比,并且還支持預(yù)覽。
但我還是會(huì)希望能夠通過腳本來處理,一方面可定制性更強(qiáng),一方面更方便整合到整個(gè)自動(dòng)化的流程鏈中。于是我又拿出了python試圖寫點(diǎn)什么,誰知道……
pngquant的命令行方式略坑……h(huán)elp中的參數(shù)說明和實(shí)際效果不一致,已經(jīng)發(fā)現(xiàn)的問題有
1. --force 參數(shù)無效,只要輸出文件存在,就會(huì)報(bào)錯(cuò),無視這個(gè)本用來指定覆寫的參數(shù)
2. --skip-if-larger 參數(shù)不正常,有時(shí)候生成文件明明比較小,也會(huì)被skip掉……
不過好在python大法好,這些問題雖然命令行本身不能處理,但python可以在上層處理掉,下面就是目前實(shí)際使用的遞歸處理某文件夾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)
- python實(shí)現(xiàn)圖片批量壓縮
- python 實(shí)現(xiàn)圖片批量壓縮的示例
- python 無損批量壓縮圖片(支持保留圖片信息)的示例
- python如何實(shí)現(xiàn)圖片壓縮
- Python無損壓縮圖片的示例代碼
- python3 圖片 4通道轉(zhuǎn)成3通道 1通道轉(zhuǎn)成3通道 圖片壓縮實(shí)例
- python實(shí)現(xiàn)圖片壓縮代碼實(shí)例
- python實(shí)現(xiàn)圖片批量壓縮程序
- Python實(shí)現(xiàn)批量壓縮圖片
- python中學(xué)習(xí)K-Means和圖片壓縮
- python利用Guetzli批量壓縮圖片
- python 批量壓縮圖片的腳本
相關(guān)文章
pytorch 如何把圖像數(shù)據(jù)集進(jìn)行劃分成train,test和val
這篇文章主要介紹了pytorch 把圖像數(shù)據(jù)集進(jìn)行劃分成train,test和val的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05python高階函數(shù)map()和reduce()實(shí)例解析
這篇文章主要介紹了python高階函數(shù)map()和reduce()實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03解決安裝Python的第三方庫pandas報(bào)錯(cuò)問題
這篇文章主要介紹了解決安裝Python的第三方庫pandas報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Python enumerate函數(shù)遍歷數(shù)據(jù)對(duì)象組合過程解析
這篇文章主要介紹了Python enumerate函數(shù)遍歷數(shù)據(jù)對(duì)象組合過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Flask-Docs自動(dòng)生成Api文檔安裝使用教程
這篇文章主要為大家介紹了Flask-Docs自動(dòng)生成Api文檔安裝使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10linux下python使用sendmail發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了linux下python使用sendmail發(fā)送郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05使用wxpy實(shí)現(xiàn)自動(dòng)發(fā)送微信消息功能
這篇文章主要介紹了使用wxpy實(shí)現(xiàn)自動(dòng)發(fā)送微信消息功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02