如何在Linux命令行(CLI)上压缩PNG图像
pngquant是一个命令行实用程序和用于PNG图像的有损压缩的库。
pngquant可以将PNG图像的大小降低到70%,同时保留完整的alpha透明度。
生成的图像与所有Web浏览器和操作系统兼容。
PNGQUANT的特征
用于实时处理/大量图像的快速模式。
使用矢量量化算法的组合的高质量调色板生成.AESY与shell脚本,GUI和服务器端软件。
单位自适应抖动算法,该探测器对图像增加了较少的噪点比标准的弗洛伊德 - 斯坦伯格。
在Linux上安装PNGQUANT
可以从OS上游存储库或者通过从源代码编译它来安装PNGQUANT。
从来源构建PGQUANT的优势在于我们可以获得最新版本,而不是系统存储库上可用的包。
在CentOS/Fedora上安装PNGQUANT
首先安装所需的依赖项。
sudo yum -y install git libpng-devel gcc cmake
然后从Git克隆Pngquant项目。
git clone --recursive https://github.com/kornelski/pngquant.git
运行 ./configure和 make在当前目录中生成PNGQUANT可执行文件。
cd pngquant ./configure make
如果我们想安装它系统范围,请运行命令:
sudo make install
二进制文件将被复制到 /usr/local/bin/pngquant
$pngquant --version 2.12.2 (November 2016)
在Ubuntu/debian上安装pngquant
安装必需的依赖项
sudo apt-get update sudo apt-get install -y git gcc cmake libpng-dev pkg-config
克隆Pngquant.
git clone --recursive https://github.com/kornelski/pngquant.git
通过运行下面的命令来安装它
cd pngquant ./configure make sudo make install
在Arch Linux/Manjaro上安装Pngquant
使用Pacman命令可以安装最新版本的PNGQUANT。
sudo pacman -S pngquant
在Linux CLI上使用PGQUANT
我们可以使用所有的PNGQUANT选项 --help选项。
$pngquant --help
pngquant, 2.12.2 (November 2016), by Kornel Lesinski, Greg Roelofs.
Compiled with no support for color profiles. Using libpng 1.6.34.
usage: pngquant [options] [ncolors] -- pngfile [pngfile ...]
pngquant [options] [ncolors] - >stdout <stdin
options:
--force overwrite existing output files (synonym: -f)
--skip-if-larger only save converted files if they're smaller than original
--output file destination file path to use instead of --ext (synonym: -o)
--ext new.png set custom suffix/extension for output filenames
--quality min-max don't save below min, use fewer colors below max (0-100)
--speed N speed/quality trade-off. 1=slow, 4=default, 11=fast & rough
--nofs disable Floyd-Steinberg dithering
--posterize N output lower-precision color (e.g. for ARGB4444 output)
--strip remove optional metadata (default on Mac)
--verbose print status messages (synonym: -v)
Quantizes one or more 32-bit RGBA PNGs to 8-bit (or smaller) RGBA-palette.
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).
If you pass the special output path "-" and a single input file, that file
will be processed and the quantized image will go to stdout.
The default behavior if the output file exists is to skip the conversion;
use --force to overwrite. See man page for full list of options.
例1
在我的笔记本电脑中,我有一个 3.9 MBPNG图像,我用Pngquant压缩。
$du -sh wallpaper-01.png 3.9M wallpaper-01.png
用pngquant压缩图像的基本示例是:
$pngquant --force --quality=40-100 --strip --skip-if-larger \ --verbose wallpaper-01.png wallpaper-01.png: read 3893KB file used gAMA and cHRM chunks to transform image to sRGB colorspace made histogram…231676 colors found selecting colors…14% selecting colors…28% selecting colors…100% moving colormap towards local minimum eliminated opaque tRNS-chunk entries…0 entries transparent mapped image to new colors…MSE=6.201 (Q=79) writing 256-color image as wallpaper-01-fs8.png Quantized 1 image.
检查目标文件的大小`
$du -sh wallpaper-01-fs8.png 1.4M wallpaper-01-fs8.png
你可以看到大小是 1.4 MB。
这是减少 2.5 MB,令人印象深刻。
如果要使用Convered One覆盖原始文件,请使用 --output file选项。
$pngquant --force --quality=40-100 --strip --skip-if-larger \ --verbose --output wallpaper-01.png wallpaper-01.png wallpaper-01.png: read 3893KB file used gAMA and cHRM chunks to transform image to sRGB colorspace made histogram…231676 colors found selecting colors…6% selecting colors…12% selecting colors…50% selecting colors…87% selecting colors…100% moving colormap towards local minimum eliminated opaque tRNS-chunk entries…0 entries transparent mapped image to new colors…MSE=6.111 (Q=79) writing 256-color image as wallpaper-01.png Quantized 1 image.
确认:
$du -sh wallpaper-01.png 1.4M wallpaper-01.png
示例2:压缩多个PNG图像
如果在当前文件夹中,我们可以使用多个PNG图像,我们可以使用BASH for循环来压缩所有项目。
for i in *.png; do pngquant --force --quality=40-100 --strip --skip-if-larger \ --verbose $i done
示例3:搜索目录中的所有PNG图像并压缩。
我们也可以使用Linux find命令以在指定目录中定位所有PNG图像并压缩然后内联。
find /mysite/wp-content/uploads/-type f -iname '*.png' -exec \
pngquant --force --quality=40-100 --skip-if-larger --strip --verbose {} --output {} \;
代替 /mysite/wp-content/uploads/通过递归搜索的路径。

