-
Notifications
You must be signed in to change notification settings - Fork 857
mtx.toBuffer and options
Called on a matrix object, this function encodes the image to an image format as a nodejs buffer. Supports synchronous or asynchronous operation.
call style:
var buf = img.toBuffer( OptionalOptionsOject );
or
img.toBuffer( function(err, img){}, OptionalOptionsOject );
Called on a matrix object, this function encodes the image to an image format as a nodejs buffer, providing the results in a callback asynchronously. Processing is done in a new worker thread. Called by toBuffer if the first argument is a function (so this function is actually not required.... as toBuffer may be used).
call style:
img.toBufferAsync( function(err, img){}, OptionalOptionsOject );
This is the (optional) object that has all the options for writing a matrix to a buffer. Recognised options are 'ext', 'jpgQuality' and 'pngCompression'.
style:
var options = { ext: '.jpg', jpgQuality: 75, pngCompression:4 };
ext
This is the format it should save to as a string of the extension. default '.jpg'. Be sure to include the dot. Supported formats are:
- .bmp
- .dib
- .jpeg*
- .jpg*
- .jpe*
- .jp2*
- .png*
- .pbm
- .pgm
- .ppm
- .sr
- .ras
- .tiff*
- .tif*
The formats with a * are not always available, for more information, see the OpenCV docs:
jpegQuality
The quality of the JPEG image after compression. Can be anywhere from 0 to 100, the default is 95.
pngCompression
The amount of compression for a PNG, it goes from 0 to 9. The default is 3.
###Examples:
// default synchronous convert to .jpg @ quality 95
var buff = image.toBuffer();
// synchronous convert to .jpg @ quality 75
var buff = image.toBuffer( { ext: ".jpg", jpegQuality: 75 } );
// default asynchronous convert to .jpg @ quality 95
image.toBuffer( function(err, buff){ /* do something with buff */ } );
// asynchronous convert to .png @ compression 4
image.toBuffer(
function(err, buff){ /* do something with buff */ },
{ ext: ".png", pngCompression: 4 }
);