Open coding-to-music opened 3 years ago
https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh
libSquoosh is an experimental way to run all the codecs you know from the Squoosh web app directly inside your own JavaScript program. libSquoosh uses a worker pool to parallelize processing images. This way you can apply the same codec to many images at once.
libSquoosh is currently not the fastest image compression tool in town and doesn’t aim to be. It is, however, fast enough to compress many images sufficiently quick at once.
libSquoosh can be installed to your local project with the following command:
$ npm install @squoosh/lib
You can start using the libSquoosh by adding these lines to the top of your JS program:
import { ImagePool } from '@squoosh/lib';
const imagePool = new ImagePool();
This will create an image pool with an underlying processing pipeline that you can use to ingest and encode images. The ImagePool constructor takes one argument that defines how many parallel operations it is allowed to run at any given time. By default, this number is set to the amount of CPU cores available in the system it is running on.
You can ingest a new image like so:
const imagePath = 'path/to/image.png';
const image = imagePool.ingestImage(imagePath);
The ingestImage
function can take anything the node readFile
function can take, uncluding a buffer and FileHandle
.
The returned image
object is a representation of the original image, that you can now preprocess, encode, and extract information about.
When an image has been ingested, you can start preprocessing it and encoding it to other formats. This example will resize the image and then encode it to a .jpg
and .jxl
image:
await image.decoded; //Wait until the image is decoded before running preprocessors.
const preprocessOptions = {
resize: {
enabled: true,
width: 100,
height: 50,
}
}
await image.preprocess(preprocessOptions);
const encodeOptions = {
mozjpeg: {}, //an empty object means 'use default settings'
jxl: {
quality: 90,
},
}
await image.encode(encodeOptions);
The default values for each option can be found in the codecs.ts
file under defaultEncoderOptions
. Every unspecified value will use the default value specified there. Better documentation is needed here.
You can run your own code inbetween the different steps, if, for example, you want to change how much the image should be resized based on its original height. (See Extracting image information to learn how to get the image dimensions).
When you have encoded everything you need, it is recommended to close the processing pipeline in the ImagePool. This will not delete the images you have already encoded, but it will prevent you from ingesting and encoding new images.
Close the ImagePool pipeline with this line:
await imagePool.close();
When you have encoded an image, you normally want to write it to a file.
This example takes an image that has been encoded as a jpg
and writes it to a file:
const rawEncodedImage = (await image.encodedWith.mozjpeg).binary;
fs.writeFile('/path/to/new/image.jpg', rawEncodedImage);
This example iterates through all encoded versions of the image and writes them to a specific path:
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,
);
}
Information about a decoded image is available at Image.decoded
. It looks something like this:
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
}
Information about an encoded image can be found at Image.encodedWith[encoderName]
. It looks something like this:
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
}
libSquoosh has an experimental auto optimizer that compresses an image as much as possible, trying to hit a specific Butteraugli target value. The higher the Butteraugli target value, the more artifacts can be introduced.
You can make use of the auto optimizer by using “auto” as the config object.
const encodeOptions: {
mozjpeg: 'auto',
}
https://github.com/GoogleChromeLabs/squoosh/tree/dev/cli
Squoosh CLI is an experimental way to run all the codecs you know from the Squoosh web app on your command line using WebAssembly. The Squoosh CLI uses a worker pool to parallelize processing images. This way you can apply the same codec to many images at once.
Squoosh CLI is currently not the fastest image compression tool in town and doesn’t aim to be. It is, however, fast enough to compress many images sufficiently quick at once.
The Squoosh CLI can be used straight from the command line without installing using npx
:
$ npx @squoosh/cli <options...>
Of course, you can also install the Squoosh CLI:
$ npm i -g @squoosh/cli
$ squoosh-cli <options...>
Usage: squoosh-cli [options] <files...>
Options:
-V, --version output the version number
-d, --output-dir <dir> Output directory (default: ".")
-s, --suffix <suffix> Append suffix to output files (default: "")
--max-optimizer-rounds <rounds> Maximum number of compressions to use for auto optimizations (default: "6")
--optimizer-butteraugli-target <butteraugli distance> Target Butteraugli distance for auto optimizer (default: "1.4")
--resize [config] Resize the image before compressing
--quant [config] Reduce the number of colors used (aka. paletting)
--rotate [config] Rotate image
--mozjpeg [config] Use MozJPEG to generate a .jpg file with the given configuration
--webp [config] Use WebP to generate a .webp file with the given configuration
--avif [config] Use AVIF to generate a .avif file with the given configuration
--jxl [config] Use JPEG-XL to generate a .jxl file with the given configuration
--wp2 [config] Use WebP2 to generate a .wp2 file with the given configuration
--oxipng [config] Use OxiPNG to generate a .png file with the given configuration
-h, --help display help for command
The default values for each config
option can be found in the codecs.ts
file under defaultEncoderOptions
. Every unspecified value will use the default value specified here. Better documentation is needed here.
Squoosh CLI has an experimental auto optimizer that compresses an image as much as possible, trying to hit a specific Butteraugli target value. The higher the Butteraugli target value, the more artifacts can be introduced.
You can make use of the auto optimizer by using “auto” as the config object.
$ npx @squoosh/cli --wp2 auto test.png
Squoosh!
https://github.com/GoogleChromeLabs/squoosh
https://squoosh.app/
Squoosh is an image compression web app that allows you to dive into the advanced options provided by various image compressors.
API & CLI
Squoosh now has an API and a CLI that allows you to compress many images at once.
Privacy
Google Analytics is used to record the following:
Image compression is handled locally; no additional data is sent to the server.
Building locally
Clone the repo, and:
You can run the development server with: