不少网站都对上传图片的大小有限制。如开源中国的博客限制图片大小在200KB之内。可是图片的大小很容易超过200KB,这就须要让咱们想办法压缩下图片的空间。算法
这几天我拍摄了不少花草树木主题的照片,我用的手机型号是华为荣耀7。shell
以一张手机拍摄的照片为例, 照片具备以下参数:windows
名称: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。api
若是用画图工具(mspaint.exe,版本6.1,内部版本7601: Service Pack 1)打开此程序,并进行另存为。mspaint一共能够将图片保存为八种格式,包括:bash
一、单色位图(扩展名bmp),1832KB,将原图转换为黑白图片服务器
二、16色位图(扩展名bmp),7286KB,压缩幅度较大,颜色严重失真app
三、256色位图(扩展名bmp),14582KB,压缩幅度较大,颜色严重失真less
四、24位位图(扩展名bmp),43712KBide
五、GIF图像(扩展名gif),6413KB工具
六、JPEG图像(扩展名jpg),3702KB
七、PNG图像(png格式),27353KB
八、TIFF图像(tif格式),19745KB
可见,mspaint保存为各类格式,图片的大小都不会过小,由于mspaint并无改变图像的尺寸。
若是想缩减图像的大小,还有一个简单的办法,就是用QQ截图把从手机端发来图像,点开后再截一遍。QQ截图支持保存四种图像类型:
一、BMP格式,大小510KB
二、JPEG格式,大小822KB
三、PNG格式,大小365KB
四、GIF格式,大小113KB
(由于是用鼠标截的,图片的尺寸长宽有±2的偏差,约在387x706)
从上面的数据能够看到,QQ截图对图像进行了压缩,效果已经很是明显,由于QQ截图计算的图像大小是按用户截图时截到的像素数计算的。不过不少图片咱们在上传时只须要让它按正常大小显示时清晰就能够了,看图片的人也不须要对其进行放大等操做以便更细致的观察,因此用QQ截图从新截取图片并另存为PNG格式也不失为给图片“减肥”的方法之一。GIF格式的压缩效果是最明显的,不过另存为这种格式的图片看上去效果并很差,我用Firefox打开了这个GIF文件看了下,部分颜色出现了严重的失真。
为了进一步对图像进行使人满意的压缩,我发现了一个工具,名叫pngquant
根据该工具官方介绍,该工具备如下四点特性(下方括号内为个人翻译):
一、High-quality palette generation using a combination of vector quantization algorithms.
(使用一组矢量量化算法制做的高质量调色盘生成器)
二、Unique adaptive dithering algorithm that adds less noise to images than the standard Floyd-Steinberg.
(独特的自适应抖动算法,较标准的佛洛依德-斯坦伯格方法噪点更少)
三、Easy to integrate with shell scripts, GUIs and server-side software.
(易于集成到Shell脚本、GUI界面和其余服务器端软件)
四、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 <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, 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批处理文件,已经能傻瓜式的解决大部分的图片转换问题了。
一、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
二、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图片进行压缩。
最后再提一个工具:PNGoo,这个工具为pngquant添加了一个GUI界面
下载地址:https://pngquant.org/PNGoo.0.1.1.zip
或是:https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/pngoo/PNGoo0.1.0.zip
抱着试试看的心态,我从第一个地址下载了此工具,发现它的libs\pngquanti目录中自带了pngquant工具(pngquanti.exe),此pngquant工具的版本为1.8.1(第二个地址我没下载成功,不过上传时间记录的是Jul 7, 2009,也算是个七年前的老古董了)。
该程序使用方法也很简单,步骤以下:
一、点击“Add”按钮新增图片到列表
二、在Output区域设定输出文件到原PNG同一目录或是指定目录
三、在CompressionOption区域设定输出PNG文件的一些参数,点击Colour按钮后进入界面以下图所示:
四、最后点击“Go!”按钮生成图片,生成后图片的Status会变为“Complete: PNGQuant used”
总结一下,其实工具PNGoo的目的就是为了方便人使用pngquant工具,但一来pngquant工具其实熟悉了命令行参数后并不难使用,二来PNGoo对应的pngquant工具已经落后于如今2.7.2不少个版本,因此这里仍是建议直接经过命令行或本身写个bat/shell脚原本使用pngquant工具。
END