Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Invalid File Type #10

Open
ngothikieuvuong opened this issue May 10, 2017 · 5 comments
Open

Error: Invalid File Type #10

ngothikieuvuong opened this issue May 10, 2017 · 5 comments

Comments

@ngothikieuvuong
Copy link

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
Copy link
Member

zeke commented May 10, 2017

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
Copy link
Author

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
Copy link
Member

zeke commented May 11, 2017

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
Copy link
Author

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
Copy link
Member

zeke commented May 12, 2017

can you drag and drop your png file here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants