国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 17:47:26
字體:
來源:轉載
供稿:網友

說到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<piao.polar@gmail.com> ----------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)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莫力| 绥中县| 永修县| 夏河县| 洪湖市| 巴中市| 迭部县| 成都市| 汶川县| 行唐县| 清新县| 朝阳市| 荣昌县| 鹿泉市| 竹山县| 阳春市| 临泽县| 文水县| 金湖县| 永修县| 张家港市| 屏南县| 阜宁县| 衡水市| 二手房| 台南县| 白朗县| 贺州市| 灵川县| 庆元县| 邯郸县| 阳江市| 西吉县| 偏关县| 苍山县| 潜江市| 工布江达县| 巴马| 子洲县| 遵化市| 三门峡市|