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

首頁 > 編程 > Python > 正文

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

2020-02-23 00:37:16
字體:
供稿:網(wǎng)友

說到png圖片壓縮,可能很多人知道TinyPNG這個網(wǎng)站。但PS插件要錢(雖然有破解的),Developer API要連到他服務器去,不提網(wǎng)絡傳輸速度,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)相當好用了,直接把文件夾往里面拽就能遞歸處理,支持各種形式的生成方式(改名、覆蓋、存儲到其他目錄等),壓縮結(jié)束給出壓縮比,并且還支持預覽。

    但我還是會希望能夠通過腳本來處理,一方面可定制性更強,一方面更方便整合到整個自動化的流程鏈中。于是我又拿出了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.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)            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 江阴市| 恩施市| 永兴县| 三河市| 浪卡子县| 阿拉尔市| 法库县| 娄烦县| 镇远县| 锡林郭勒盟| 射阳县| 嫩江县| 平原县| 隆回县| 云霄县| 武宁县| 涡阳县| 平遥县| 平安县| 安仁县| 饶河县| 巩留县| 遂川县| 井研县| 商洛市| 望奎县| 萍乡市| 洞头县| 潮州市| 温宿县| 留坝县| 湾仔区| 岳普湖县| 曲靖市| 宕昌县| 通城县| 辽宁省| 西吉县| 太湖县| 阳朔县| 饶平县|