colorjs / get-image-colors

Extract colors from GIF, PNG, JPG, and SVG images
346 stars 46 forks source link

Error: Invalid File Type #10

Open ngothikieuvuong opened 7 years ago

ngothikieuvuong commented 7 years ago

Hi, I have an issue when using getColors, it log error: "Error: Invalid File Type" I am writing the automation scripts in synchronous style with Chimp. My scripts is as below:

var buffer = fs.readFileSync(path.join(__dirname, '/../../resources/uploadData/', '1.png'));
getColors(buffer, function(error, colors){
    if (err) throw err; // it doesn't log error
    console.log(buffer); // Log: <Buffer 89 50 4e 47 0d 0a ....>
    console.log(error); // Log "Error: Invalid File Type"
});

Please help me where I am wrong? Thanks!

zeke commented 7 years ago

Hi @vuongngolg !

I see a few issues:

if (err) throw err; is not throwing because the argument is called error

When using a buffer, you need to specify its mime type:

getColors(buffer, 'image/gif').then(colors => {
  // `colors` is an array of color objects
})

But you don't have to use a buffer if you don't want to specify the mimetype. You can just pass a fully-qualified filename:

const path = require('path')
const getColors = require('get-image-colors')

getColors(path.join(__dirname, 'double-rainbow.png')).then(colors => {
  // `colors` is an array of color objects
})

See https://github.com/zeke/get-image-colors#usage for more info. Hope this helps.

ngothikieuvuong commented 7 years ago

Thanks @zeke

I've tried 2 ways as following:

    var color;
    var path = require('path');
    return color = getColors(path.join(__dirname, '/../resources/uploadData/', '1.png')).then(color => {
        console.log(color); // Log: [Color {_rgb: [239, 238, 237, 1]} ,... ]
    });

    var path = require('path');
    getColors(path.join(__dirname, '/../resources/uploadData/', '1.png')).then(color => {
        console.log("running"); // Log nothing, it doesn't run into this block codes
    });

The first way can run into step and log color, but the second way cannot. I don't know why. Could you help me to explain? Anyway, I am applying the first way. Thanks

zeke commented 7 years ago

Try this:

var path = require('path')
var file = path.join(__dirname, '/../resources/uploadData/', '1.png')

console.log(file)
// does that look like the right file path?

getColors(file)
.catch(err => {
  throw err
})
.then(color => {
  console.log(colors)
})
ngothikieuvuong commented 7 years ago

Thanks, I tried exactly the same, but it doesn't work, see comment in line

var path = require('path') var file = path.join(__dirname, '/../resources/uploadData/', '1.png')

console.log(file) // does that look like the right file path? --> Yes, it logs the right path, and the file is existing on this path.

getColors(file) .catch(err => { throw err //does not log error }) .then(color => { console.log(colors) // does not log anything })

zeke commented 7 years ago

can you drag and drop your png file here?