简体   繁体   中英

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.

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

I don't know what wrong, but with this library does not works for me. Then I use:

https://www.npmjs.com/package/jimp

and it works for me easily. Maybe it help someone.

const Jimp = require('jimp');

try {
  const readFile = await Jimp.read(filePath)
  await readFile.writeAsync(pathToConvertedFile)
  const buffPng = await fs.readFile(pathToConvertedFile);
} catch (err) {
  console.log(err);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM