在Python中使用pngquant壓縮png圖片的教程
說(shuō)到png圖片壓縮,可能很多人知道TinyPNG這個(gè)網(wǎng)站。但PS插件要錢(雖然有破解的),Developer API要連到他服務(wù)器去,不提網(wǎng)絡(luò)傳輸速度,Key也是有每月限制的。
但是貌似tinyPNG是使用了來(lái)自于 pngquant 的技術(shù),至少在 http://pngquant.org/ 中是如此聲稱的:TinyPNG and Kraken.io — on-line interfaces for pngquant。如果真是這樣,我很想對(duì)TinyPNG說(shuō)呵呵。后者是開(kāi)源的,連首頁(yè)中提供的GUI工具也都是開(kāi)源的。并且TinyPNG在首頁(yè)的原理說(shuō)明里面,一次都沒(méi)提到pngquant
我取了tinyPNG的首頁(yè)上的示例圖用pngquant命令行跑了一下,壓縮率和顯示效果差不多。
pngquant首頁(yè)上提供的工具中,Pngyu(http://nukesaq88.github.io/Pngyu/)是跨平臺(tái)并且開(kāi)源的,個(gè)人覺(jué)得已經(jīng)相當(dāng)好用了,直接把文件夾往里面拽就能遞歸處理,支持各種形式的生成方式(改名、覆蓋、存儲(chǔ)到其他目錄等),壓縮結(jié)束給出壓縮比,并且還支持預(yù)覽。
但我還是會(huì)希望能夠通過(guò)腳本來(lái)處理,一方面可定制性更強(qiáng),一方面更方便整合到整個(gè)自動(dòng)化的流程鏈中。于是我又拿出了python試圖寫點(diǎn)什么,誰(shuí)知道……
pngquant的命令行方式略坑……h(huán)elp中的參數(shù)說(shuō)明和實(shí)際效果不一致,已經(jīng)發(fā)現(xiàn)的問(wèn)題有
1. --force 參數(shù)無(wú)效,只要輸出文件存在,就會(huì)報(bào)錯(cuò),無(wú)視這個(gè)本用來(lái)指定覆寫的參數(shù)
2. --skip-if-larger 參數(shù)不正常,有時(shí)候生成文件明明比較小,也會(huì)被skip掉……
不過(guò)好在python大法好,這些問(wèn)題雖然命令行本身不能處理,但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 無(wú)損批量壓縮圖片(支持保留圖片信息)的示例
- python如何實(shí)現(xiàn)圖片壓縮
- Python無(wú)損壓縮圖片的示例代碼
- 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)文章
Python訪問(wèn)OPCUA服務(wù)器,訂閱一個(gè)變量標(biāo)簽方式
這篇文章主要介紹了Python訪問(wèn)OPCUA服務(wù)器,訂閱一個(gè)變量標(biāo)簽方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
python?pytorch圖像識(shí)別基礎(chǔ)介紹
大家好,本篇文章主要講的是python?pytorch圖像識(shí)別基礎(chǔ)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Tensorflow之MNIST CNN實(shí)現(xiàn)并保存、加載模型
這篇文章主要為大家詳細(xì)介紹了Tensorflow之MNIST CNN實(shí)現(xiàn)并保存、加載模型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Django 查詢數(shù)據(jù)庫(kù)返回JSON的實(shí)現(xiàn)
和前端交互全部使用JSON,如何將數(shù)據(jù)庫(kù)查詢結(jié)果轉(zhuǎn)換成JSON格式,本文就來(lái)介紹一下,感興趣的小伙伴們可以參考一下2021-08-08
使用python的pandas讀取excel文件中的數(shù)據(jù)詳情
這篇文章主要介紹了使用python的pandas讀取excel文件中的數(shù)據(jù)詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
pyqt5 實(shí)現(xiàn)工具欄文字圖片同時(shí)顯示
今天小編就為大家分享一篇pyqt5 實(shí)現(xiàn)工具欄文字圖片同時(shí)顯示的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
如何解決pycharm中用matplotlib畫圖不顯示中文的問(wèn)題
這篇文章主要介紹了如何解決pycharm中用matplotlib畫圖不顯示中文的問(wèn)題,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06
3分鐘學(xué)會(huì)一個(gè)Python小技巧
Python時(shí)間日期轉(zhuǎn)換在開(kāi)發(fā)中是非常高頻的一個(gè)操作,你經(jīng)常會(huì)遇到需要將字符串轉(zhuǎn)換成 datetime 或者是反過(guò)來(lái)將 datetime 轉(zhuǎn)換成字符串,今天小編給大家?guī)?lái)了一個(gè)Python小技巧,感興趣的朋友一起看看吧2018-11-11

