使用pngquant批量壓縮png

pngquant 是一個(gè)批次轉(zhuǎn)換數(shù)值化和高頻振動(dòng)真彩PNG圖像的實(shí)用程序,特別是那些有一個(gè)完整的alpha通道,歸結(jié)為“rgba調(diào)色板”。這些圖像通常是兩到四次小于整個(gè)32位版本,和局部透明度是很好地保存下來。這使得pngquant特別是有用的對(duì)網(wǎng)站和PlayStation 2發(fā)展,其中一個(gè)紋理格式是基于rgba調(diào)色板(盡管不是png壓縮)。
終端輸入 pngquant -h 參數(shù)如下:

usage:  pngquant [options] [ncolors] [pngfile [pngfile ...]]

options:
  --force           overwrite existing output files (synonym: -f)
  --nofs            disable Floyd-Steinberg dithering
  --ext new.png     set custom suffix/extension for output filename
  --speed N         speed/quality trade-off. 1=slow, 3=default, 10=fast & rough
  --quality min-max don't save below min, use less colors below max (0-100)
  --verbose         print status messages (synonym: -v)
  --iebug           increase opacity to work around Internet Explorer 6 bug
  --transbug        transparent color will be placed at the end of the palette

Quantizes one or more 32-bit RGBA PNGs to 8-bit (or smaller) RGBA-palette
PNGs using Floyd-Steinberg diffusion dithering (unless disabled).
The output filename is the same as the input name except that
it ends in "-fs8.png", "-or8.png" or your custom extension (unless the
input is stdin, in which case the quantized image will go to stdout).
The default behavior if the output file exists is to skip the conversion;
use --force to overwrite.

其中-f參數(shù)已經(jīng)驗(yàn)證無效,參數(shù)說明可以參照官網(wǎng)里面。
開發(fā)中壓圖用默認(rèn)設(shè)置基本上可以了。
壓縮目錄下所有png圖片實(shí)例:

import os

# 獲取指定路徑下所有指定后綴的文件
# dir 指定路徑
# ext 指定后綴,鏈表&不需要帶點(diǎn) 或者不指定。例子:['xml', 'java']
def GetFileFromThisRootDir(dir, ext = None):
    allfiles = []
    needExtFilter = (ext != None)
    for root,dirs,files in os.walk(dir):
        for filespath in files:
            filepath = os.path.join(root, filespath)
            extension = os.path.splitext(filepath)[1][1:]
            if needExtFilter and extension == ext in ext:
                allfiles.append(filepath)
    return allfiles

if __name__ == '__main__':
    PngquantExe="/Users/*/PycharmProjects/QTPY/pngquant"
    srcDir="/Users/*/Desktop/cutimg"
    imgFiles=GetFileFromThisRootDir(srcDir, 'png')
    suffix="_temp.png"
    for f in imgFiles:
        cmd = "\"" + PngquantExe + "\"" + " --ext " + suffix + " --force --speed=3 "+ f
        os.system(cmd)
        os.remove(f)
        newfile=f.replace(".png", suffix)
        os.rename(newfile, f)

ps:遙想當(dāng)初用PhotoShop壓圖真是慘不忍睹。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • pngquant:下載地址:https://pngquant.org/ pngquant 是一個(gè)批次轉(zhuǎn)換數(shù)值化和高...
    babybus_newapp閱讀 7,074評(píng)論 0 1
  • 版本記錄 前言 只要是做圖片的或者與圖片相關(guān)的,那么圖片的格式就是一個(gè)不可以繞過的問題,我們見過很多的圖片格式,但...
    刀客傳奇閱讀 88,371評(píng)論 0 22
  • 本文譯自YUI官方博客 本文主要介紹圖片的格式以及如何在開發(fā)工作中選擇正確的圖像格式。我們先簡短地介紹比較流行的g...
    mcat閱讀 2,401評(píng)論 0 8
  • 有個(gè)朋友調(diào)侃我 說你當(dāng)初就不應(yīng)該那么努力讀書 沒有勇氣去開始一段感情 看看你現(xiàn)在圈子多小 活該你單身!(好啦 后半...
    木子嘉嘉LJ閱讀 335評(píng)論 0 2
  • 一、cell自適應(yīng)高度 設(shè)置的AutoLayout 約束必須讓 cell 的 contentView知道如何自動(dòng)伸...
    wq04200閱讀 169評(píng)論 0 0