Skip to content

mtx.toBuffer and options

btsimonh edited this page Nov 4, 2017 · 3 revisions

toBuffer

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 );

toBufferAsync

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 );

BufferOptions

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:

http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#Mat%20imread%28const%20string&%20filename,%20int%20flags%29


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 }
);
Clone this wiki locally