Get dominant image color node js

I am trying to get the dominant color of the image that I have on my computer. Before using this code, I set imagemagick and the dominant color. I get the result undefined and 000000. Why is this happening? What have I done wrong?

var AWS = require('aws-sdk'),
fs = require('fs');
var express = require("express");
var app = express();

const resizeImg = require('resize-img');
var color = require('dominant-color'),
    imgPath = './Users/ADIBAR/Desktop/cloud/beach_life-normal.jpg';

color(imgPath,function(err,color){
    console.log(color);
})

color(imgPath,{format:'rgb'},function(err,color){
    console.log(color);
})

// For dev purposes only
AWS.config.update({ accessKeyId: '', secretAccessKey: '' });

var fileStream = fs.createReadStream('beach_life-normal.jpg');
fileStream.on('error', function (err) {
  if (err) { throw err; }
});  

fileStream.on('open', function () {
  var s3 = new AWS.S3();




resizeImg(fs.readFileSync('beach_life-normal.jpg'), {width: 128, height:128}).then(buf => {
    fs.writeFileSync('beach_life-normal-new.jpg', buf);
    //upload//
    s3.putObject({
      Bucket: 'adinoauploadefile',
      Key: 'beach_life-normal-new.jpg',
      Body: 'beach_life-normal-new.jpg'
    }, function (err) {
     if (err) { throw err; }
    });
  });  
});
+4
source share
1 answer

It looks like your problem is with the path to the image - either use the absolute path:

imgPath = 'C:/[path to your file]/Users/ADIBAR/Desktop/cloud/beach_life-normal.jpg';

or relative (put the image in the same folder as your script):

imgPath = './beach_life-normal.jpg
0
source

All Articles