在node中使用 PngQuant

1. 依赖准备

  • pngquant - npm (npmjs.com)

    npm i pngquant
    
  • memory-streams - npm (npmjs.com)

    npm i memory-streams
    

2. 使用方法

官方示例:

var PngQuant = require('pngquant'),myPngQuanter = new PngQuant([192, '--quality', '60-80', '--nofs', '-']);sourceStream.pipe(myPngQuanter).pipe(destinationStream);

官方示例非常简约,很多人一看就直接晕了,特别是都没接触过 MemoryStream 的人,我也是在官方的github仓库中看到了相关内容才知道需要用 MemoryStream 的,所以上面依赖我就推荐安装两个了,若果有别的 MemoryStream也可以用别的。下面展示我的demo:

const fs = require('fs');
const path = require('path');
const PngQuant = require('pngquant');
const MemoryStream = require('memory-stream');let sourceImg = path.join(__dirname, '原图 与当前js文件所在目录的相对路径');
let compressedImg = path.join(__dirname, '压缩后图 与当前js文件所在目录的相对路径');const sourceStream = fs.createReadStream(sourceImg);
const destinationStream = new MemoryStream();const myPngQuanter = new PngQuant([192, '--quality', '60-80', '--nofs', '-']);sourceStream.pipe(myPngQuanter).pipe(destinationStream);// 完成压缩的图片转为buffer然后保存为图片
destinationStream.on('finish', function () {// 把buffer写入到图片文件fs.writeFile(compressedImg, destinationStream.toBuffer(), function (err) {if (err) {console.log(err);}});
});

压缩后的png图片效果很明显,有80%多的压缩率,而且图片肉眼看基本不失真,压缩算法很不错。

3. 命令分析

node 中使用 pngquant 时,传入的一个数组参数是一个 options ,其实相当于使用命令行时输入的命令参数。
示例中使用的参数,转换成命令行应该是:

pngquant --quality=60-80 --nofs=-

在命令行使用时,语法是这样的:

# 1
pngquant [options] [ncolors] -- pngfile [pngfile ...]
# 2
pngquant [options] [ncolors] - >stdout <stdin
usage:  pngquant [options] [ncolors] [pngfile [pngfile ...]]options:--force           overwrite existing output files (synonym: -f)--nofs            disable Floyd-Steinberg dithering--ext new.png     set custom suffix/extension for output filename--speed N         speed/quality trade-off. 1=slow, 3=default, 10=fast & rough--quality min-max don't save below min, use less colors below max (0-100)--verbose         print status messages (synonym: -v)--iebug           increase opacity to work around Internet Explorer 6 bug--transbug        transparent color will be placed at the end of the paletteQuantizes one or more 32-bit RGBA PNGs to 8-bit (or smaller) RGBA-palette
PNGs using Floyd-Steinberg diffusion dithering (unless disabled).
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.

所以想要增加配置,直接在New QngQuant 中增加即可

在node中使用 PngQuant相关推荐

  1. 长连接及在Node中的应用——HTTP/1.1 keep-alive

    HTTP请求都要经过TCP三次握手建立连接,四次分手断开连,如果每个HTTP请求都要建立TCP连接的话是极其费时的,因此HTTP/1.1中浏览器默认开启了Connection: keep-alive. ...

  2. 打开浏览器的包 node_如何发布可在浏览器和Node中使用的软件包

    打开浏览器的包 node When you create a package for others to use, you have to consider where your user will ...

  3. node中模块、AMD与CMD、ES6模块,node中使用ES6

    1.Nodejs 中的模块 在node环境中一个js文件就是一个模块(module) 我们采用的是CommonJS规范,使用require引入模块,使用module.exports导出接口 node的 ...

  4. node 进阶 | 通过node中如何捕获异常阐述express的特点

    node如何捕获异常 node基于js的单线程,有了非阻塞异步回调的概念,但是在处理多个并发连接时,并发环境要求高,最重要的是单线程,单核CPU,一个进程crash则web服务都crash,但是为什么 ...

  5. Node中同步与异步的方式读取文件

    场景 Node.js最大的特点就是异步式I/O(或者非阻塞I/O)与事件紧密结合的编程模式.这种模式与传统的同步式I/O线性的编程思路有很大的不同,因为控制流很大程度上要靠事件和回调函数来组织,一个逻 ...

  6. 二十五、Node中的Buffer缓冲器和EventEmitter事件触发器

    @Author:Runsen @Date:2020/6/5 作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件.导致翘课严重,专业排名 ...

  7. node中异步IO的理解

    解释性语言和编译型语言的区别: 计算器不能直接的理解高级语言,只能理解机器语言,所以必须把高级语言翻译为机器语言,翻译的方式有两种,一个是编译,一个是解释. 解释性语言的程序不需要编译,它是在运行程序 ...

  8. node --- 在node中使用mongoosemongoDB的安装

    *首先确保,你的电脑安装了mongodb,网址: mongodb官网 *使用npm安装 mongoose: mongoose官网 ps:mongoose是Node中操作mongoDB的第三方插件.用于 ...

  9. 什么流读取MultipartFile_深入理解并运用Node中的IO模型流

    在 NodeJs 中,流随处可见,读/写文件流,HTTP请求/返回流,stdin/stdout流.理解并运用好流会让你的Node更具力量. Stream lib/_stream_readable.js ...

最新文章

  1. 《Drupal实战》——1.9 小结
  2. 电视机当计算机屏幕,电视机可以当电脑显示器吗
  3. JS浏览器加载一个页面的过程
  4. 微信小程序 全局变量异步函数_微信小程序【生命周期】
  5. mPaaS:全新移动开发平台,只为打造性能更优越的App
  6. php读这文件速度,php 测试硬盘读写-php 测试硬盘写速率
  7. [RCNN]-[YOLO]-[SSD]目标检测算法
  8. swt 键盘事件ctrl+c_VB键盘事件详解
  9. python虚拟环境可以运行pyspark_pyspark使用自定义的python
  10. Java21天打卡练习Day21-集合map
  11. win11系统安装打印机的方法
  12. USB-C 端口在您的 Mac 上无法使用如何解决?
  13. 解决求平均值出现加和导致的溢出问题
  14. DelayQueue使用
  15. drop_caches释放哪些内存
  16. 基于秃鹰搜索算法的无线传感器网络三维覆盖优化
  17. 三维空间几何变换矩阵
  18. 玩转RFID(一) - MFRC522模块上手
  19. http://www.hi-donet.com/网站
  20. 第一类第二类斯特林数总结

热门文章

  1. 起点小说字体加密python TTFont解析流
  2. 故障:不能登录到“纳税人权益平台”
  3. 2010-10-12 VOA special English 01
  4. 机器学习 --- 核方法(Kernel Method)
  5. 非常好用的底部导航栏
  6. cross frequency coupling
  7. 胡小兔的 PKUSC2018 游记
  8. express的学习
  9. CCS v3.3在win10电脑上的详细安装步骤
  10. wps文档复制粘贴序号_wps 中word文档插入表格,如何插入的表格中实现 序号的自动 填写...