Toolchain for building webassembly executables
353
Resize images in browser without pixelation and reasonably fast. Autoselect the best of available technologies: webworkers, webassembly, createImageBitmap, pure JS.
With pica you can:
Here is a short list of problems you can face:
Access-Control-Allow-Origin header.exif header to get proper orientation. Images can be rotated.canvas.toBlob() method.
Use pica.toBlob(), it includes required shim.exif data, to avoid rotation info loss.
The most simple way is to cut original header and glue it to
resized result. Look here
for examples.node.js (to develop, build via browserify and so on):
npm install pica
bower:
bower install pica
Attention!. Compiled files are in /dist folder! If you wish to
load module in node.js style as require('pica') - your project MUST
be compiled with Broserify to properly use Web Workers. In other
case - use require('pica/dist/pica').
Create resizer instance with given config.
const pica = require('pica')({
tile: 1024, // Tile width/height. Images are processed
// by regions, to restrict peak memory use.
features: [ 'all' ], // Mostly for testing, restricts
// available features. Can be:
// [ 'js', 'wasm', 'cib', 'ww' ] or [ 'all' ]
idle: 2000, // Cache timeout, ms. Webworkers create
// is not fast. This option allow reuse
// webworkers effectively.
concurrency: 4 // Max webworkers pool size. Default is
// autodetected CPU count, but not more than 4.
});
// Resize from Canvas/Image to another Canvas
pica.resize(from, to, {
unsharpAmount: 80,
unsharpRadius: 0.6,
unsharpThreshold: 2
})
.then(result => console.log('resize done!'));
// Resize & convert to blob
pica.resize(from, to)
.then(result => pica.toBlob(result, 'image/jpeg', 90))
.then(blob => console.log('resized to canvas & created blob!'));
Resize image from one canvas (or image) to another. Sizes are taken from source and destination objects.
3 (lanczos, win=3).false.0 (off). Usually
between 50 to 100 is good.0. Threshold for
applying unsharp mask.Result is Promise, resolved with to on success.
(!) If you need to process multiple images, do it sequentially to optimize CPU & memory use. Pica already knows how to use multiple cores (if browser allows).
Convenience method, similar to canvas.toBlob(), but with
promise interface & polyfill for old browsers.
Supplementary method, not recommended for direct use. Resize Uint8Array with raw RGBA bitmap (don't confuse with jpeg / png / ... binaries). It does not use tiles & webworkers. Left for special cases when you really need to process raw binary data (for example, if you decode jpeg files "manually").
3 (lanczos, win=3).false.0 (off).
Usually between 50 to 100 is good.0. Threshold
for applying unsharp mask.pica to create new one.Result is Promise, resolved with resized rgba buffer.
Pica has presets to adjust speed/quality ratio.
Simply use quality option param:
In real world you will never need to change default (max) quality. All this variations were implemented to better understand resize math :)
Ifter scale down image can look a bit blured. It's good idea to sharpen it
a bit. Pica has built-in "unsharp mask" filter (off by default).
Set unsharpAmount to positive number to activate the filter.
Filter's parameters are similar to ones from Photoshop.
We recommend to start with unsharpAmount = 80,
unsharpRadius = 0.6 and unsharpThreshold = 2.
There is a correspondence between UnsharpMask parameters
in popular graphics software.
We didn't have time to test all possible combinations, but in general:
Note. Though you can run this package on node.js, browsers
are the main target platform. On server side we recommend to use
sharp.
You can find these links useful:
Content type
Image
Digest
Size
230.4 MB
Last updated
over 9 years ago
docker pull arodin/pica-toolchain