使用pngquant批量压缩png

  • pngquant:
    下载地址:https://pngquant.org/

pngquant 是一个批次转换数值化和高频振动真彩PNG图像的实用程序,特别是那些有一个完整的alpha通道,归结为“rgba调色板”。这些图像通常是两到四次小于整个32位版本,和局部透明度是很好地保存下来。这使得pngquant特别是有用的对网站和PlayStation 2发展,其中一个纹理格式是基于rgba调色板(尽管不是png压缩)。
终端输入 pngquant -h 参数如下:

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参数已经验证无效,参数说明可以参照官网里面。
开发中压图用默认设置基本上可以了。
压缩目录下所有png图片实例:

import os

# 获取指定路径下所有指定后缀的文件
# dir 指定路径
# ext 指定后缀,链表&不需要带点 或者不指定。例子:['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:遥想当初用PhotoShop压图真是惨不忍睹。

你可能感兴趣的:(使用pngquant批量压缩png)