node.jsgm

Convert tiff to jpeg in node.js and GraphicsMagick


How could I convert a tiff to jpg in node.js and GraphicsMagick gm (https://aheckmann.github.io/gm/)?

I want to do this on AWS lambda, so cant write() out to disk as such.


Solution

  • Simply specify the file extension you want to the .write() method and gm will convert it automatically in that format.

    const gm = require('gm');
    
    gm('sample.tiff')
      .write('out.jpeg', function (err) {
        if (err) console.log(err);
    }); 
    

    If you need to output as buffer instead of writing to disk, you can use .toBuffer() method:

    gm('sample.tiff')
      .toBuffer('jpeg', function (err, buffer) {
        if (err) console.log(err);
    });