Eddy
Eddy

Reputation: 51

How to change color image (jpg,jpeg,png) to black and white or grayscale in node js

I used all npm package that support grayscaling but nothing is working properly. Some of them are working but quality reduced. there is a good grayscale package named image-grayscale but problem is that when one of the src image (example/image.gpg) file is corrupted then according to promise code stopped to go further. Below code is the problem.

 globby(['./upload/*.*','!./upload/*.ico','!./upload/*.gif', '!./upload/*.txt']).then(function (paths) {
            return Promise.all(paths.map(function (e) {
                    return imageGrayScale(e, {logProgress: 1})                                      }));
                        }).then(function (val) {


   // if one of the file in directory is corrupted then promise is rejected and my code stooped  and i cant do  anything further

 })

Please tell me how to handle error promise to code go further. or is there any solution like callback.

should i go to any other module or can written own algorithim for grayscale please tell me how to convert color of image to black and white

Upvotes: 3

Views: 7868

Answers (1)

Rohan Malcolm
Rohan Malcolm

Reputation: 368

I have used Jimp before with success so you could look into that.

An example taken from the documentation

Jimp.read("file.png", (err, image) => {
    if (err) throw err;
    image.greyscale().write("image.png");
});

Upvotes: 7

Related Questions