自由语
利布斯库什是一个 实验 相关的 JavaScript 后端 CLI 软件包。当你 在您自己的项目中可能使用此库, 请注意它不是 被认为已准备就绪供一般使用,没有立即计划 {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}这样就能做到 如果您使用这个包,您会冒着风险这样做.
libSquoosh使用工人池来平行地处理图像. 这边请 能够同时对许多图像应用相同的编码器.
libSquoosh目前不是镇上最快的图像压缩工具 并不打算成为。 但是,它足够快 压缩许多图像 足够快的一次.
请注意,这是叉子 原始图书馆,已经 从退休以来 这个叉子继续工作,并有一些错误修正.
安装
libSquoosh 可以安装到您的本地工程, 命令如下:
$ npm install @frostoven/libsquoosh
你可以开始使用libSquoosh,在你的JS计划中添加这些行:
import { ImagePool } from '@frostoven/libsquoosh';
import { cpus } from 'os';
const imagePool = new ImagePool(cpus().length);
这将创建一个图像池,其中包含一个基本的处理管道,可以用来摄取并编码图像. ImagePool 构造器采用一个参数,定义它允许在任何特定时间运行多少个平行操作. 默认情况下,这个数字被设定为它运行的系统中可用的CPU核心的数量.
插入图像
你可以摄取这样的新形象:
import fs from 'fs/promises';
const file = await fs.readFile('./path/to/image.png');
const image = imagePool.ingestImage(imagePath);
这个 ingestImage 函数可以接受任意 矩阵缓冲 是否来自 readFile() 或者说 fetch()。 。 。 .
回来的 image 对象是原始图像的表达式,您现在可以对图像进行预处理、编码和提取信息.
预处理和编码图像
当一个图像被摄入后,可以开始预处理并编码到其他格式. 此示例将修改图像大小并编码为 .jpg 财务报告和审定财务报表 .jxl 图像 :
await image.decoded; //Wait until the image is decoded before running preprocessors.
const preprocessOptions = {
//When both width and height are specified, the image resized to specified size.
resize: {
enabled: true,
width: 100,
height: 50,
},
/*
//When either width or height is specified, the image resized to specified size keeping aspect ratio.
resize: {
enabled: true,
width: 100,
}
*/
};
await image.preprocess(preprocessOptions);
const encodeOptions = {
mozjpeg: {}, //an empty object means 'use default settings'
jxl: {
quality: 90,
},
};
await image.encode(encodeOptions);
每个选项的默认值可见于 codecs.ts 文件编号 defaultEncoderOptions。每个未指定值将使用指定的默认值. 这里需要更好的文件.
您可以在不同的步骤之间运行自己的代码, 例如, 如果您想改变图像的大小, 以它最初的高度为基础 。 (见 正在提取图像信息 学习如何获得图像维度).
关闭图像Pool
当您已经编码了您需要的一切时, 建议关闭图像Pool中的处理管道 。 这将不会删除您已经编码的图像, 但是它会防止您摄取和编码新图像 .
关闭此线的 ImagePool 管道 :
await imagePool.close();
将编码的图像写入文件系统
当您已经编码了图像时, 您通常想要将其写入文件 .
此示例以编码为 jpg 并将其写入文件 :
const rawEncodedImage = (await image.encodedWith.mozjpeg).binary;
fs.writeFile('/path/to/new/image.jpg', rawEncodedImage);
此示例通过所有编码的图像版本, 将其写入特定路径 :
const newImagePath = '/path/to/image.'; //extension is added automatically
for (const encodedImage of Object.values(image.encodedWith)) {
fs.writeFile(
newImagePath + (await encodedImage).extension,
(await encodedImage).binary,
);
}
正在提取图像信息
有关已解码图像的信息,请访问 Image.decoded看起来像这样
console.log(await image.decoded);
// Returns:
{
bitmap: {
data: Uint8ClampedArray(47736584) [
225, 228, 237, 255, 225, 228, 237, 255, 225, 228, 237, 255,
225, 228, 237, 255, 225, 228, 237, 255, 225, 228, 237, 255,
225, 228, 237, 255,
... //the entire raw image
],
width: 4606, //pixels
height: 2591 //pixels
},
size: 2467795 //bytes
}
关于被编码的图像的信息可见于 Image.encodedWith[encoderName]看起来像这样
console.log(await image.encodedWith.jxl);
// Returns:
{
optionsUsed: {
quality: 75,
baseline: false,
arithmetic: false,
progressive: true,
... //all the possible options for this encoder
},
binary: Uint8Array(1266975) [
1, 0, 0, 1, 0, 1, 0, 0, 255, 219, 0, 132,
113, 119, 156, 156, 209, 1, 8, 8, 8, 8, 9, 8,
9, 10, 10, 9,
... //the entire raw encoded image
],
extension: 'jxl',
size: 1266975 //bytes
}
自动优化器
利布斯库什有一个 实验 尽可能压缩图像的自动优化器,试图击中特定 黄油 目标值。 巴特劳格利的目标值越高,可以引进的文物就越多.
通过使用“自动”作为配置对象,您可以使用自动优化器.
const encodeOptions: {
mozjpeg: 'auto',
}