Resize images in browser without pixelation and reasonably fast. Autoselect the best of available technologies: webworkers, webassembly, createImageBitmap, pure JS.
With pica you can:
Note. If you need File/Blob resize (from form's file input), consider use image-blob-reduce. It has additional machinery to process orientation, keep EXIF metadata and so on.
Multiply unsharpAmount
by 2, divide unsharpThreshold
by 2, example:
pica@6
: pica.resize(a, b, { unsharpAmount: 80, unsharpThreshold: 2 })
pica@7
: pica.resize(a, b, { unsharpAmount: 160, unsharpThreshold: 1 })
Here is a short list of problems you can face:
Access-Control-Allow-Origin
header.canvas.toBlob()
method.
Use pica.toBlob()
, it includes required shim.exif
data. Consider use
image-blob-reduce.npm install pica
const pica = require('pica')();
// Resize from Canvas/Image to another Canvas
pica.resize(from, to)
.then(result => console.log('resize done!'));
// Resize & convert to blob
pica.resize(from, to)
.then(result => pica.toBlob(result, 'image/jpeg', 0.90))
.then(blob => console.log('resized to canvas & created blob!'));
Create resizer instance with given config (optional):
[ 'js', 'wasm', 'ww' ]
. Can be [ 'js', 'wasm', 'cib', 'ww' ]
or [ 'all' ]
. Note, cib
is buggy in Chrome and not supports default
mks2013
filter.Important! Latest browsers may support resize via createImageBitmap.
This feature is supported (cib
) but disabled by default and not recommended
for use. So:
createImageBitmap()
is used for non-blocking image decode (when available,
without downscale).cib
will depend on your browser. Result
without cib
will be predictable and good.Resize image from one canvas (or image) to another. Sizes are taken from source and destination objects.
Canvas
, Image
or ImageBitmap
..filter
instead) - 0..3.mks2013
). See resize_filter_info.js for details. mks2013
does both resize and sharpening, it's optimal and not recommended to change.0
(off). Usually
value between 100 to 200 is good. Note, mks2013
filter already does
optimal sharpening.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").
.filter
instead) - 0..3.mks2013
). See resize_filter_info.js for details. mks2013
does both resize and sharpening, it's optimal and not recommended to change.0
(off). Usually
value between 100 to 200 is good. Note, mks2013
filter already does
optimal sharpening.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 :)
After 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 = 160
,
unsharpRadius = 0.6
and unsharpThreshold = 1
.
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:
Available as part of the Tidelift Subscription.
The maintainers of pica and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.