当前位置: 首页 > 工具软件 > pngquant > 使用案例 >

pngquant&tinypng 使用教程

顾磊
2023-12-01

介绍

pngquant是一个命令行工具和一个用于有损压缩PNG图像的库。转换显着减少文件大小(通常高达70%),并保留完整的alpha透明度。生成的图像与所有网络浏览器和操作系统兼容。

官网https://pngquant.org/

GitHub源码https://github.com/kornelski/pngquant

下载

forWindowshttps://pngquant.org/pngquant-windows.zip

forMachttps://pngquant.org/pngquant.tar.bz2

安装使用

Windows和mac下都是无需安装的,可以直接使用。

mac: 终端下进入到下载的文件夹,在当前目录行执行./pngquant [参数] file 即可。

Windows: 进入下载的文件夹,可以看到两个后缀为.bat脚本,文件名就是它的功能。直接拖拽需要压缩的图片到该文件上,即可自行压缩。

参数

See pngquant -h for full list.

pngquant -h 查看完整的选项列表

--quality min-max

min and max are numbers in range 0 (worst) to 100 (perfect), similar to JPEG. pngquant will use the least amount of colors required to meet or exceed the max quality. If conversion results in quality below the min quality the image won’t be saved (if outputting to stdin, 24-bit original will be output) and pngquant will exit with status code 99.

min和max是范围从0(最差)到100(完美)的数字,类似于JPEG。pngquant将使用最少的颜色来达到或超过最大的质量。如果转换结果的质量低于最低质量,图像将不会被保存(如果输出到stdin,将输出24位的原始图像),pngquant退出并返回状态码99。

    pngquant --quality=65-80 image.png

--ext new.png

Set custom extension (suffix) for output filename. By default -or8.png or -fs8.png is used. If you use --ext=.png --force options pngquant will overwrite input files in place (use with caution).

    pngquant --ext=_new.png image.png
    pngquant --ext=.png --f image.png

为输出文件设置自定义扩展名(后缀),默认是-or8.png-fs8.png。如果你使用--ext=.png --force选项,pngquant会覆盖输入文件(使用时要小心)。

-o out.png or --output out.png

Writes converted file to the given path. When this option is used only single input file is allowed.

将转换后的文件输出到指定路径下。这个参数只支持单个输入文件。

--skip-if-larger

Don’t write converted files if the conversion isn’t worth it.

如果转换后的文件不值得,则不输出。(只保留比源文件小的转换文件)

--speed N

Speed/quality trade-off from 1 (slowest, highest quality, smallest files) to 11 (fastest, less consistent quality, light comperssion). The default is 4. It’s recommended to keep the default, unless you need to generate images in real time (e.g. map tiles). Higher speeds are fine with 256 colors, but don’t handle lower number of colors well.

速度-质量转换参数从1(时间最长、质量最高、文件最小)到11(速度最快、质量不一致、轻压缩)。默认参数是4。建议保持默认值,除非需要实时生成图像(例如地图文件)。高速下处理256颜色效果不错,但是不能很好处理较少的颜色。

--nofs

Disables Floyd-Steinberg dithering.

禁止Floyd-Steinberg抖动。

--floyd=0.5

Controls level of dithering (0 = none, 1 = full). Note that the = character is required.

控制抖动级别(0=无,1=满)。注意,’ = '字符是必需的。

--posterize bits

Reduce precision of the palette by number of bits. Use when the image will be displayed on low-depth screens (e.g. 16-bit displays or compressed textures in ARGB444 format).

降低调色板精度的位数。当图像显示在低深度屏幕上时使用(例如16位显示器或ARGB444格式的压缩纹理)。

--strip

Don’t copy optional PNG chunks. Metadata is always removed on Mac (when using Cocoa reader).

不要复制可选的PNG块。元数据在Mac上总是被删除(当使用Cocoa reader时)

实用工具

shell脚本,批量压缩目录下所有文件

#!/bin/bash

# 需要手动修改两个变量 

# pngquant 脚本路径
pngquant_path='./pngquant.exe'
targetFile='./res'

# png 图片后缀
suffix='.png' 

# 压缩 png
function compressPng(){
	file=$1 #参数1
	# echo "----- compress "$file
	if [ -f $file ]; then
		#判断文件是否以 指定字段结尾
		echo $file | grep -q -E $suffix 
		if [ $? -eq 0 ]; then # 0找到 1没找到 >1错误 ,-q:不输出
			#执行压缩
			`${pngquant_path} --ext=.png -f $file`
			flag=$?
			if [ $flag -eq 0 ]; then # 压缩成功
				echo "succeed-----"$file 
			else
				echo "failed------"$file 
			fi
		fi
	fi
}


# 递归文件夹
function recursion_dir(){
	dir=$1
	cd $dir

	# 遍历
	for fileName in `ls`; do
		if [ -f $fileName ]; then
			compressPng $fileName
		elif [ -d $fileName ]; then
			recursion_dir $fileName
		fi
	done

	cd ..
}


# 入口函数
function main(){
	#只对复制后的目录操作
	if [ -d $targetFile ]; then
		local newName=${targetFile}'/../temp-pngquant'
		cp -r $targetFile ${newName} # 复制的文件保存到 目标文件同级
		echo 'Compression begins ...'$(date)
		recursion_dir $newName
		echo 'Compression is over ...'$(date)
	else
		echo 'Erro: Please input target dir full path !'
	fi
}

# execute
main

tinypng

使用pngquant压缩图片时,如果转换结果的质量低于最低质量,图像将不会被保存,这个时候就出现压缩失败的情况了。这里再介绍一种在线压缩图片的工具:tinypng。使用方法很简单,网页上有介绍:

英文网: https://tinypng.com/

中文网:https://tinify.cn

批量使用教程:使用介绍

 类似资料: