1
votes

Try to use imagemagick npm module.

Need to convert a .png file with transparent background to .jpeg with white background.

Example:

const ImageMagick = require('imagemagick');

ImageMagick.convert(
  [
    'source_file.png', 
    'destination_file.jpg'
  ],
  function(err, stdout){}
);

The result: destination file is saved with black background.

Didn't find needed option to resolve this issue.

P.S. For example PHP can do this by using imagecolorallocate() and imagefill() functions.

1

1 Answers

2
votes

You can pass arguments in the convert function like so:

ImageMagick.convert(
  [
    'source_file.png', 
    '-background',
    'rgb(255,255,255)',
    '-flatten',
    'destination_file.jpg'
  ],
  function(err, stdout){}
);

Check source here: https://www.npmjs.com/package/imagemagick#convertargs-callbackerr-stdout-stderr