压缩png命令cmd_使用pngquant工具对PNG格式图片进行压缩

很多网站都对上传图片的大小有限制。如开源中国的博客限制图片大小在200KB以内。但是图片的大小很容易超过200KB,这就需要让我们想办法压缩下图片的空间。

这几天我拍摄了很多花草树木主题的照片,我用的手机型号是价格1999的华为荣耀7。

以一张手机拍摄的照片为例, 照片具有如下参数:

名称:IMG_20160716_135420

型号:PLK-AL10

大小:5.86MB

宽度:2896

高度:5152

曝光:1/33s

ISO:200

光圈:2.0

焦距:27mm(等效35mm胶片焦距)

白平衡:自动

闪光灯:未使用闪光灯

路径:/storage/emulated/0/DCIM/Camera/IMG_20160716_135420.jpg

打开手机版QQ(Android,V6.5.0.2835)按原图的形式将图片发送到电脑端的QQ(QQ7.4(15203))。电脑端QQ接收到相片后,将相片另存为成jpg文件,宽度和高度都没有变,还是2896x5152,大小变为2.21MB。

如果用画图工具(mspaint.exe,版本6.1,内部版本7601: Service Pack 1)打开此程序,并进行另存为。mspaint一共可以将图片保存为八种格式,包括:

1、单色位图(扩展名bmp),1832KB,将原图转换为黑白图片

2、16色位图(扩展名bmp),7286KB,压缩幅度较大,颜色严重失真

3、256色位图(扩展名bmp),14582KB,压缩幅度较大,颜色严重失真

4、24位位图(扩展名bmp),43712KB

5、GIF图像(扩展名gif),6413KB

6、JPEG图像(扩展名jpg),3702KB

7、PNG图像(png格式),27353KB

8、TIFF图像(tif格式),19745KB

可见,mspaint保存为各种格式,图片的大小都不会太小,因为mspaint并没有改变图像的尺寸。

如果想缩减图像的大小,还有一个简单的办法,就是用QQ截图把从手机端发来图像,点开后再截一遍。QQ截图支持保存四种图像类型:

1、BMP格式,大小510KB

2、JPEG格式,大小822KB

3、PNG格式,大小365KB

4、GIF格式,大小113KB

(因为是用鼠标截的,图片的尺寸长宽有±2的误差,约在387x706)

从上面的数据可以看到,QQ截图对图像进行了压缩,效果已经非常明显,因为QQ截图计算的图像大小是按用户截图时截到的像素数计算的。不过很多图片我们在上传时只需要让它按正常大小显示时清晰就可以了,看图片的人也不需要对其进行放大等操作以便更细致的观察,所以用QQ截图重新截取图片并另存为PNG格式也不失为给图片“减肥”的方法之一。GIF格式的压缩效果是最明显的,不过另存为这种格式的图片看上去效果并不好,我用Firefox打开了这个GIF文件看了下,部分颜色出现了严重的失真。

为了进一步对图像进行令人满意的压缩,我发现了一个工具,名叫pngquant

根据该工具官方介绍,该工具有以下四点特性(下方括号内为我的翻译):

1、High-quality palette generation using a combination of vector quantization algorithms.

(使用一组矢量量化算法制作的高质量调色盘生成器)

2、Unique adaptive dithering algorithm that adds less noise to images than the standard Floyd-Steinberg.

(独特的自适应抖动算法,较标准的佛洛依德-斯坦伯格方法噪点更少)

3、Easy to integrate with shell scripts, GUIs and server-side software.

(易于集成到Shell脚本、GUI界面和其他服务器端软件)

4、Fast mode for processing large numbers of images.

(对大量图像采取快速模式处理)

我们直接从官方网站下载最新版本的pngquant即可,2016年7月17日可下载到的最新版本是2.7.2。

下载下来的一个zip压缩包:pngquant-windows.zip

解压缩此压缩包后,可以看到文件列表如下:

pngquant.exe就是我们要用到的命令行工具,用cmd执行此程序(不带参数)时会有如下说明信息:

E:\pngquant>pngquant.exe

pngquant, 2.7.2 (June 2016), by Kornel Lesinski, Greg Roelofs.

SSE acceleration disabled.

Compiled with OpenMP (multicore support).

Compiled with no support for color profiles. Using libpng 1.6.23.

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

pngquant [options] [ncolors] - >stdout

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, 3=default, 11=fast & rough

--nofs disable Floyd-Steinberg dithering

--posterize N output lower-precision color (e.g. for ARGB4444 output)

--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).

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.

E:\pngquant>

这里面详细说明了pngquant工具的各命令行参数,不过我们可以先学着使用这个工具最简单的功能,把我们之前的那张2.21MB的JPEG图像翻出来,用QQ截图重新截一下,保存成PNG格式(380KB),再输入如下命令使用pngquant进行处理:

Microsoft Windows [版本 6.1.7601]

版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\Tsybius>e:

E:\>cd pngquant

E:\pngquant>pngquant.exe --force --verbose C:\Users\Tsybius\Desktop\test_pic.png

C:\Users\Tsybius\Desktop\test_pic.png:

read 381KB file

made histogram...51684 colors found

selecting colors...3%

selecting colors...6%

selecting colors...10%

selecting colors...31%

selecting colors...51%

selecting colors...55%

selecting colors...75%

selecting colors...79%

selecting colors...82%

selecting colors...100%

moving colormap towards local minimum

eliminated opaque tRNS-chunk entries...0 entries transparent

mapped image to new colors...MSE=1.867 (Q=93)

writing 256-color image as C:\Users\Tsybius\Desktop\test_pic-fs8.png

No errors detected while quantizing 1 image.

E:\pngquant>

命令执行完毕后,生成了图像test_pic-fs8.png,这个图像只有175KB。哈哈,开源中国的博客只能上传200KB以下的图片,所以这张原本很大的照片就可以上传上来了,这张照片拍的是在北京南礼士路行道树上周氏啮小蜂的孕蜂蛹(难怪月坛这片儿找不到蝴蝶,哼哼)。

其实,pngquant工具自带的两个bat批处理文件,已经能傻瓜式的解决大部分的图片转换问题了。

1、Drag PNG here to reduce palette automatically.bat

@echo off

set path=%~d0%~p0

:start

"%path%pngquant.exe" --force --verbose --quality=45-85 %1

"%path%pngquant.exe" --force --verbose --ordered --speed=1 --quality=50-90 %1

shift

if NOT x%1==x goto start

2、Drag PNG here to reduce palette to 256.bat

@echo off

set path=%~d0%~p0

:start

"%path%pngquant.exe" --force --verbose 256 %1

shift

if NOT x%1==x goto start

直接将要转换的png图片拖到这两个批处理文件上,该png图片就可以作为批处理文件的命令行参数传入,pngquant工具将会自动将png图片进行压缩。

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值