压缩png命令cmd_使用pngquant工具对PNG格式图片进行压缩
很多網站都對上傳圖片的大小有限制。如開源中國的博客限制圖片大小在200KB以內。但是圖片的大小很容易超過200KB,這就需要讓我們想辦法壓縮下圖片的空間。
這幾天我拍攝了很多花草樹木主題的照片,我用的手機型號是華為榮耀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圖片進行壓縮。
最后再提一個工具:PNGoo,這個工具為pngquant添加了一個GUI界面
抱著試試看的心態,我從第一個地址下載了此工具,發現它的libs\pngquanti目錄中自帶了pngquant工具(pngquanti.exe),此pngquant工具的版本為1.8.1(第二個地址我沒下載成功,不過上傳時間記錄的是Jul 7, 2009,也算是個七年前的老古董了)。
該程序使用方法也很簡單,步驟如下:
1、點擊“Add”按鈕新增圖片到列表
2、在Output區域設定輸出文件到原PNG同一目錄或是指定目錄
3、在CompressionOption區域設定輸出PNG文件的一些參數,點擊Colour按鈕后進入界面如下圖所示:
4、最后點擊“Go!”按鈕生成圖片,生成后圖片的Status會變為“Complete: PNGQuant used”
總結一下,其實工具PNGoo的目的就是為了方便人使用pngquant工具,但一來pngquant工具其實熟悉了命令行參數后并不難使用,二來PNGoo對應的pngquant工具已經落后于現在2.7.2很多個版本,所以這里還是建議直接通過命令行或自己寫個bat/shell腳本來使用pngquant工具。
END
總結
以上是生活随笔為你收集整理的压缩png命令cmd_使用pngquant工具对PNG格式图片进行压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 线性代数考研笔记(一)
 - 下一篇: 如何加快计算机绘图的速度,如何提高Aut